challeng模块新建、create

This commit is contained in:
huang 2017-03-06 17:36:01 +08:00
parent ca118e2bef
commit 956a078a36
17 changed files with 266 additions and 2 deletions

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the challenges controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,89 @@
class ChallengesController < ApplicationController
layout "base_shixun"
before_filter :find_shixun, :only => [:index, :new, :create, :destroy]
before_filter :find_challenge, :only => [:show, :update, :add_journal, :complete_training_task]
before_filter :authorize_tpi, :only => [:new, :create]
before_filter :build_challege_from_params, :only => [:new, :create, :update_form]
# before_filter :allow_manager, :only => [:index, :show]
# before_filter :allow_members, :only => [:new, :create]
def new
unless @shixun.parent_id.nil?
return render_403
end
# 顶部导航
@project_menu_type = 11
respond_to do |format|
format.html { render :action => 'new', :layout => 'base_shixun' }
end
end
def create
@challenge.subject = params[:challenge][:subject]
@challenge.description = params[:challenge][:description]
@challenge.position = params[:challenge][:position].to_i
@challenge.user = User.current
if @challenge.save
respond_to do |format|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun)}
end
return
else
respond_to do |format|
format.html { render :action => 'new' }
end
end
end
def index
end
def show
# 顶部导航
@project_menu_type = 11
# 打开编辑内容
respond_to do |format|
format.html
format.js
end
end
private
def build_challege_from_params
if params[:id].blank?
@challenge = Challenge.new
@challenge.shixun = @shixun
else
@challenge = @shixun.challenges.find(params[:id])
end
@challenge.shixun = @shixun
@challenge.user ||= User.current
end
def find_challenge
@challenge = Challenge.find(params[:id])
@shixun = @challenge.shixun
rescue ActiveRecord::RecordNotFound
render_404
end
# TPM:parent_id is nil
# TPI is not allowed
def authorize_tpi
unless @shixun.parent_id.nil?
render_403
end
end
# Find project of id params[:id]
def find_shixun
shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id])
@shixun = Shixun.find(shixun_id)
rescue ActiveRecord::RecordNotFound
render_404
end
end

View File

@ -0,0 +1,2 @@
module ChallengesHelper
end

26
app/models/challenge.rb Normal file
View File

@ -0,0 +1,26 @@
class Challenge < ActiveRecord::Base
belongs_to :shixun,:touch=> true
belongs_to :user
has_many :journals, :as => :journalized, :dependent => :destroy
has_many :visible_journals,
:class_name => 'Journal',
:as => :journalized,
:conditions => Proc.new {
["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false]
},
:readonly => true
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy
# has_many :forge_acts, :class_name => 'Challenge', :as => :forge_act, :dependent => :destroy
acts_as_attachable
validates_presence_of :subject
validates_length_of :subject, :maximum => 255
# after_create :act_as_forge_activity
# def act_as_forge_activity
# self.forge_acts << ForgeActivity.new(:user_id => self.user_id, :project_id => self.project_id)
# end
end

View File

@ -4,6 +4,7 @@ class Shixun < ActiveRecord::Base
has_many :users, :through => :shixun_members
has_many :shixun_members
has_one :repository
has_many :challenges, :dependent => :destroy, :order => "challenges.id ASC"
def parent
shixun = Shixun.find_by_parent_id(self.parent_id)

View File

@ -125,6 +125,7 @@ class User < Principal
has_many :contestant_work_projects
has_many :shixuns, :through => :shixun_members, :dependent => :destroy
has_many :challenges, :dependent => :destroy
has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)},
:after_remove => Proc.new {|user, group| group.user_removed(user)}

View File

@ -0,0 +1,45 @@
<ul>
<li class="clear">
<label class="label"><span class="c_red f12">*</span>&nbsp;序号&nbsp;&nbsp;:&nbsp;</label>
<%= f.text_field :position, :no_label => true, :placeholder => "请输入序号(整数)" %>
</li>
<li class="clear">
<label class="label"><span class="c_red f12">*</span>&nbsp;标题&nbsp;&nbsp;:&nbsp;</label>
<%= f.text_field :subject, :class => "input-big", :no_label => true, :id => "training_task_id", :placeholder => "请输入标题" %>
<span style="display: none">标题不能为空</span>
</li>
<li class="clear">
<label class="label fl">&nbsp;描述&nbsp;&nbsp;:&nbsp;</label>
<%= f.label_for_field :description, :no_label => true, :class => "fl" %>
<%= f.kindeditor :description,
:editor_id => "training_task_desc_editor",
:width=>'885px',
:height =>192,
:resizeType => 0,
:no_label => true,
:at_id => @shixun.id,
:at_type => @shixun.class.to_s %>
</li>
<li class="clear">
<% if params[:action] == "new" %>
<a href="<%= shixun_challenges_path(@shixun, :remote => true) %>" class="sy_btn_grey mr5 fr"> 取消</a>
<% else %>
<%= link_to "取消", challenge_path(@challenge), :class => "sy_btn_grey mr5 fr" %>
<% end %>
<input onclick="issue_create();" class="sy_btn_blue mr5 fr" onfocus="this.blur()" id="issue_confirm" style="width: 28px;color: #FFF" value="保存">
</li>
</ul>
<script type="text/javascript">
function issue_create(){
training_task_desc_editor.sync();
$('#new_challenge').submit();
}
$(function(){
$("#training_task_position").bind('keyup', function (){
this.value=this.value.replace(/\D/g,'')
});
$("#training_task_position").bind('afterpaste',function(){
this.value=this.value.replace(/\D/g,'')
});
});
</script>

View File

@ -0,0 +1,20 @@
<%= content_for(:header_tags) do %>
<%= import_ke(enable_at: true, prettify: false, init_activity: false) %>
<% end %>
<!--新建缺陷开始-->
<div class="pro_new_con mb10">
<div class="pro-task-new-con clear">
<%#= call_hook(:view_issues_new_top, {:training_task => @training_task}) %>
<%= labelled_form_for @challenge, :url => shixun_challenges_path(@shixun) do |f| %>
<%= error_messages_for 'training_task' %>
<%= hidden_field_tag 'copy_from', params[:copy_from] if params[:copy_from] %>
<div>
<%= render :partial => 'form', :locals => {:f => f} %>
</div>
<% end %>
</div>
</div>

View File

@ -0,0 +1,29 @@
<%= content_for(:header_tags) do %>
<%= import_ke(enable_at: true) %>
<%= javascript_include_tag 'create_kindeditor'%>
<% end %>
<script>
sd_create_editor_from_data(<%= @challenge.id %>, null, "100%", "<%= @challenge.class.name %>");
</script>
<div class="mt10 mb10" id =issue_show_total"">
<div class="banner-big f16 fontGrey3">
任务详情
<a href="<%= new_shixun_challenge_path(@shixun) %>" class="sy_btn_green fr" >新建</a>
</div>
<div class="container-big mt10" style="float:left;">
<div class="pro_page_box">
<div class="problem_main borderBottomNone">
<div id="issue_detail_show">
<%= @challenge.subject %>
</div>
</div>
<!--problem_main end-->
<div style="clear: both;"></div>
</div>
</div>
<div class="cl"></div>
</div>

View File

@ -1 +1 @@
<%= @shixun %>
<%= @shixun.name %>

View File

@ -39,6 +39,10 @@ RedmineApp::Application.routes.draw do
collection do
end
resources :challenges do
end
resources :repositories, :shallow => true, :except => [:index, :show] do
member do
match 'committers', :via => [:get, :post]

View File

@ -0,0 +1,14 @@
class CreateChallenges < ActiveRecord::Migration
def change
create_table :challenges do |t|
t.integer :shixun_id
t.string :subject
t.text :description
t.integer :user_id
t.integer :status, :default => 0, :limit => 1
t.integer :position, :default => 1, :limit => 1
t.timestamps
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20170303071513) do
ActiveRecord::Schema.define(:version => 20170306070453) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -329,6 +329,17 @@ ActiveRecord::Schema.define(:version => 20170303071513) do
t.datetime "updated_at", :null => false
end
create_table "challenges", :force => true do |t|
t.integer "shixun_id"
t.string "subject"
t.text "description"
t.integer "user_id"
t.integer "status", :limit => 1, :default => 0
t.integer "position", :limit => 1, :default => 1
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "changes", :force => true do |t|
t.integer "changeset_id", :null => false
t.string "action", :limit => 1, :default => "", :null => false

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe ChallengesController, :type => :controller do
end

View File

@ -0,0 +1,6 @@
FactoryGirl.define do
factory :challenge do
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Challenge, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end