Agent steps layout and content submission classes with nested attributes and polymorphism

James Peret 10 年之前
父节点
当前提交
3f5605ca82

+ 1 - 0
Gemfile

@@ -46,6 +46,7 @@ gem 'bootstrap-timepicker-rails'
46 46
 gem "bootstrap-switch-rails"
47 47
 gem 'jasny_bootstrap_extension_rails'
48 48
 gem 'simple_form'
49
+gem "nested_form"
49 50
 gem 'bootstrap_form'
50 51
 gem 'friendly_id', '~> 5.0.0'
51 52
 gem 'devise'

+ 2 - 0
Gemfile.lock

@@ -163,6 +163,7 @@ GEM
163 163
     multi_json (1.10.1)
164 164
     multi_test (0.1.1)
165 165
     multi_xml (0.5.5)
166
+    nested_form (0.3.2)
166 167
     net-scp (1.2.1)
167 168
       net-ssh (>= 2.6.5)
168 169
     net-ssh (2.9.1)
@@ -328,6 +329,7 @@ DEPENDENCIES
328 329
   letter_opener
329 330
   mini_magick
330 331
   mixpanel-ruby
332
+  nested_form
331 333
   pg
332 334
   rails (= 4.0.4)
333 335
   rails-i18n (~> 4.0.0)

+ 22 - 2
app/controllers/missions_controller.rb

@@ -79,8 +79,8 @@ class MissionsController < ApplicationController
79 79
   end
80 80
   
81 81
   def take_agent_role
82
-    mission = Mission.find(params[:id])
83
-    agent = mission.mission_agents.find(params[:agent])
82
+    mission = Mission.friendly.find(params[:id])
83
+    agent = mission.mission_agents.friendly.find(params[:agent])
84 84
     candidate = MissionCandidate.create!(user: current_user, mission_agent: agent, status: 'closed', mode: 'take_agent_role')
85 85
     agent.user = current_user
86 86
     respond_to do |format|
@@ -93,6 +93,21 @@ class MissionsController < ApplicationController
93 93
       end
94 94
     end
95 95
   end
96
+  
97
+  def step_submission
98
+    mission = Mission.friendly.find(params[:id])
99
+    agent = mission.mission_agents.friendly.find(params[:agent])
100
+    submission = StepSubmission.new(step_submission_params)
101
+    respond_to do |format|
102
+      if submission.save
103
+        format.html { redirect_to mission_agent_details_path(mission, agent), notice: (t 'mission.step_submission_confirmation') }
104
+        format.json { head :no_content }
105
+      else
106
+        format.html { redirect_to mission_agent_details_path(mission, agent), alert: (t 'mission.step_submission_error') }
107
+        format.json { render json: @mission.errors, status: :unprocessable_entity }
108
+      end
109
+    end
110
+  end
96 111
 
97 112
   private
98 113
     # Use callbacks to share common setup or constraints between actions.
@@ -104,4 +119,9 @@ class MissionsController < ApplicationController
104 119
     def mission_params
105 120
       params.require(:mission).permit(:mission_agents_id, :title, :objective, :briefing, :owner_id, :status, :launched, :language, :cover_img, :slug, :end_date)
106 121
     end
122
+    
123
+    def step_submission_params
124
+      params.require(:step_submission).permit(:agent_step_id, :validated, :validated_by_id, :date_validated, :created_at, :updated_at, :language, :cover_img, :slug, :end_date, :submission_contents_attributes => [:submission_content_id, :submission_type, :created_at, :updated_at, :submission_attributes => [:submission_content_id, :content, :accepted, :validation_id, :created_at, :updated_at]])
125
+    end
126
+    
107 127
 end

+ 3 - 1
app/models/step_submission.rb

@@ -1,5 +1,7 @@
1 1
 class StepSubmission < ActiveRecord::Base
2 2
   belongs_to :agent_step  
3 3
   belongs_to :validated_by, :class_name => "User"
4
-  has_many :submission_contents
4
+  has_many :submission_contents, :dependent => :destroy
5
+  has_many :submissions, :through => :submission_contents
6
+  accepts_nested_attributes_for :submission_contents, allow_destroy:true
5 7
 end

+ 2 - 1
app/models/submission_content.rb

@@ -1,4 +1,5 @@
1 1
 class SubmissionContent < ActiveRecord::Base
2
-  belongs_to :agent_step
2
+  belongs_to :step_submission
3 3
   belongs_to :submission, polymorphic: true
4
+  accepts_nested_attributes_for :submission, allow_destroy:true
4 5
 end

+ 1 - 2
app/models/submission_text.rb

@@ -1,5 +1,4 @@
1 1
 class SubmissionText < ActiveRecord::Base
2
-  belongs_to :submission_content
3
-  has_one :submission_content, as: :submission, dependent: :destroy
4 2
   belongs_to :validation
3
+  has_one :submission_content, :as => :submission, dependent: :destroy
5 4
 end

+ 11 - 1
app/views/missions/_step_submission.html.erb

@@ -14,9 +14,19 @@
14 14
 		<% end %>
15 15
 		<%= content_tag(:div, class: 'panel-body collapse', id: validation_id) do %>
16 16
 			<%= content_tag(:div, class: 'panel-content') do %>
17
+					
18
+				<% # Form %>
17 19
 				<%= content_tag(:div, class: "panel-text") do %>
18
-					<p>sdfsdfsdfsdfdsf</p>
20
+					
21
+					<%= f.fields_for :submission_contents, SubmissionContent.new, show_empty: true do |f| %>
22
+						<%= f.alert_message "Please fix the errors below." %>
23
+						<%= f.fields_for :submission_text, SubmissionText.new, show_empty: true do |f| %>
24
+							<%= f.text_field :content %>
25
+						<% end %>
26
+					<% end %>
27
+
19 28
 				<% end %>
29
+				
20 30
 			<% end %>
21 31
 		<% end %>
22 32
 	<% end %>

+ 22 - 15
app/views/missions/show_agent_details.html.erb

@@ -75,24 +75,31 @@
75 75
 			<%= content_tag(:h4, ((t 'agent.tasks') + ':') ) %>
76 76
 			<% # Steps %>
77 77
 			<% @agent.agent_steps.order('step ASC').each do |step| %>
78
-				<%= content_tag(:div, class: 'panel panel-default') do %>
79
-					<%= content_tag(:div, class: 'panel-heading') do %>
80
-						<%= content_tag(:h3, class: 'panel-title') do %>
81
-							<%= content_tag(:span, step.step, class: 'task-number') %>
82
-							<a class="accordion-toggle" data-toggle="collapse" data-parent="#colapse_steps" href="#collapse_step_<%= step.step %>">
83
-								<span class="caret"></span>
84
-								<%= content_tag(:span, step.title, class: 'task-text') %>
85
-							</a>
78
+				<% step_submission = StepSubmission.new %>
79
+				<%= bootstrap_nested_form_for(step_submission) do |f| %>
80
+					<%= content_tag(:div, class: 'panel panel-default') do %>
81
+						<%= content_tag(:div, class: 'panel-heading') do %>
82
+							<%= content_tag(:h3, class: 'panel-title') do %>
83
+								<%= content_tag(:span, step.step, class: 'task-number') %>
84
+								<a class="accordion-toggle" data-toggle="collapse" data-parent="#colapse_steps" href="#collapse_step_<%= step.step %>">
85
+									<span class="caret"></span>
86
+									<%= content_tag(:span, step.title, class: 'task-text') %>
87
+									
88
+								</a>
89
+								<% if step.step_validations.count > 0 %>
90
+									<%= f.button :submit, class: 'btn btn-small btn-success pull-right' %>
91
+								<% end %>
86 92
 							
87
-						<% end %>
88
-					<% end %>
89
-					<%= content_tag(:div, class: 'panel-body collapse', id: ('collapse_step_'+ step.step.to_s)) do %>
90
-						<%= content_tag(:div, class: "panel-content") do %>
91
-							<%= content_tag(:div, class: "panel-text") do %>
92
-								<%= step.description.html_safe %>
93 93
 							<% end %>
94
-							<%= render :partial => 'step_submission', locals: {step: step} %>
94
+						<% end %>
95
+						<%= content_tag(:div, class: 'panel-body collapse', id: ('collapse_step_'+ step.step.to_s)) do %>
96
+							<%= content_tag(:div, class: "panel-content") do %>
97
+								<%= content_tag(:div, class: "panel-text") do %>
98
+									<%= step.description.html_safe %>
99
+								<% end %>
100
+								<%= render :partial => 'step_submission', locals: {step: step, f: f, step_submission: step_submission} %>
95 101
 							
102
+							<% end %>
96 103
 						<% end %>
97 104
 					<% end %>
98 105
 				<% end %>

+ 3 - 0
config/routes.rb

@@ -33,6 +33,9 @@ Avalanche2::Application.routes.draw do
33 33
   get 'dashboard' => 'agents#dashboard', as: :dashboard
34 34
   get 'agents' => 'agents#list', as: :agent_list
35 35
   get 'agent/:id' => 'agents#show', as: :show_agent
36
+  
37
+  # Steps
38
+  post 'missions/:id/agents/:agent/step_submission' => 'missions#step_submission', as: :step_submissions
36 39
 
37 40
   # Admin Panel
38 41
   get "admin/dashboard" => "admin_panel#dashboard", :as => :admin_dashboard

+ 6 - 0
db/migrate/20150219012955_change_submission_contents_from_submission_contents.rb

@@ -0,0 +1,6 @@
1
+class ChangeSubmissionContentsFromSubmissionContents < ActiveRecord::Migration
2
+  def change
3
+    remove_reference :submission_contents, :submission_content, index: true
4
+    add_reference :submission_contents, :step_submission, index: true
5
+  end
6
+end

+ 3 - 3
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20150218012557) do
14
+ActiveRecord::Schema.define(version: 20150219012955) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -165,14 +165,14 @@ ActiveRecord::Schema.define(version: 20150218012557) do
165 165
   add_index "step_validations", ["agent_step_id"], name: "index_step_validations_on_agent_step_id", using: :btree
166 166
 
167 167
   create_table "submission_contents", force: true do |t|
168
-    t.integer  "submission_content_id"
169 168
     t.integer  "submission_id"
170 169
     t.string   "submission_type"
171 170
     t.datetime "created_at"
172 171
     t.datetime "updated_at"
172
+    t.integer  "step_submission_id"
173 173
   end
174 174
 
175
-  add_index "submission_contents", ["submission_content_id"], name: "index_submission_contents_on_submission_content_id", using: :btree
175
+  add_index "submission_contents", ["step_submission_id"], name: "index_submission_contents_on_step_submission_id", using: :btree
176 176
 
177 177
   create_table "submission_texts", force: true do |t|
178 178
     t.integer  "submission_content_id"