Commit 6e9fe757b0438f4b75a4bb80ce6e9eddfd0971f6
1 parent
8139c736
Exists in
master
and in
1 other branch
Refactoring update to work instead of update_from_abroad
Showing
3 changed files
with
13 additions
and
10 deletions
Show diff stats
app/controllers/choices_controller.rb
| @@ -74,8 +74,6 @@ class ChoicesController < InheritedResources::Base | @@ -74,8 +74,6 @@ class ChoicesController < InheritedResources::Base | ||
| 74 | options[:builder].tag!('choice_status', the_status) } | 74 | options[:builder].tag!('choice_status', the_status) } |
| 75 | logger.info "successfully saved the choice #{@choice.inspect}" | 75 | logger.info "successfully saved the choice #{@choice.inspect}" |
| 76 | 76 | ||
| 77 | - Question.update_counters(@question.id, :inactive_choices_count => @choice.active? ? 0 : 1) | ||
| 78 | - | ||
| 79 | visitor_votes = Proc.new { |options| options[:builder].tag!('visitor_votes', visitor.votes.count(:conditions => {:question_id => @question.id})) } | 77 | visitor_votes = Proc.new { |options| options[:builder].tag!('visitor_votes', visitor.votes.count(:conditions => {:question_id => @question.id})) } |
| 80 | visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) } | 78 | visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) } |
| 81 | 79 | ||
| @@ -204,6 +202,13 @@ class ChoicesController < InheritedResources::Base | @@ -204,6 +202,13 @@ class ChoicesController < InheritedResources::Base | ||
| 204 | end | 202 | end |
| 205 | 203 | ||
| 206 | end | 204 | end |
| 205 | + | ||
| 206 | + def update | ||
| 207 | + # prevent AttributeNotFound error and only update actual Choice columns, since we add extra information in 'show' method | ||
| 208 | + choice_attributes = Choice.new.attribute_names | ||
| 209 | + params[:choice] = params[:choice].delete_if {|key, value| !choice_attributes.include?(key)} | ||
| 210 | + update! | ||
| 211 | + end | ||
| 207 | 212 | ||
| 208 | 213 | ||
| 209 | 214 |
app/models/choice.rb
| @@ -14,7 +14,12 @@ class Choice < ActiveRecord::Base | @@ -14,7 +14,12 @@ class Choice < ActiveRecord::Base | ||
| 14 | has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" | 14 | has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" |
| 15 | named_scope :active, :conditions => { :active => true } | 15 | named_scope :active, :conditions => { :active => true } |
| 16 | named_scope :inactive, :conditions => { :active => false} | 16 | named_scope :inactive, :conditions => { :active => false} |
| 17 | - | 17 | + |
| 18 | + after_save :update_questions_counter | ||
| 19 | + | ||
| 20 | + def update_questions_counter | ||
| 21 | + self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) | ||
| 22 | + end | ||
| 18 | #attr_accessor :data | 23 | #attr_accessor :data |
| 19 | 24 | ||
| 20 | def question_name | 25 | def question_name |
| @@ -105,7 +110,6 @@ class Choice < ActiveRecord::Base | @@ -105,7 +110,6 @@ class Choice < ActiveRecord::Base | ||
| 105 | def activate! | 110 | def activate! |
| 106 | (self.active = true) | 111 | (self.active = true) |
| 107 | self.save! | 112 | self.save! |
| 108 | - Question.update_counters(self.question_id, :inactive_choices_count => -1) | ||
| 109 | end | 113 | end |
| 110 | 114 | ||
| 111 | def suspend! | 115 | def suspend! |
| @@ -116,7 +120,6 @@ class Choice < ActiveRecord::Base | @@ -116,7 +120,6 @@ class Choice < ActiveRecord::Base | ||
| 116 | def deactivate! | 120 | def deactivate! |
| 117 | (self.active = false) | 121 | (self.active = false) |
| 118 | self.save! | 122 | self.save! |
| 119 | - Question.update_counters(self.question_id, :inactive_choices_count => 1) | ||
| 120 | end | 123 | end |
| 121 | 124 | ||
| 122 | protected | 125 | protected |