Skip to content

Commit

Permalink
fix deprecation warnings about calling scope() with a hash
Browse files Browse the repository at this point in the history
DEPRECATION WARNING: Calling #scope or #default_scope with a hash is deprecated. Please use a lambda containing a scope. E.g. scope :red, -> { where(color: 'red') }. (called from included at ~/surveyor/lib/surveyor/models/answer_methods.rb:13)

We use instance_eval because we need the scope lambda to run in the
context of the including Class, not the Module in which the code is
defined.
  • Loading branch information
toby cabot committed Aug 15, 2013
1 parent 50ba0fa commit ade06ea
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/surveyor/models/answer_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.included(base)
base.send :has_many, :validations, :dependent => :destroy

# Scopes
base.send :default_scope, :order => "#{base.quoted_table_name}.display_order ASC"
base.instance_eval {default_scope ->{order "#{base.quoted_table_name}.display_order ASC"}}

# Mustache
base.send :include, MustacheContext
Expand Down
2 changes: 1 addition & 1 deletion lib/surveyor/models/question_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.included(base)
base.send :belongs_to, :correct_answer, :class_name => "Answer", :dependent => :destroy

# Scopes
base.send :default_scope, :order => "#{base.quoted_table_name}.display_order ASC"
base.instance_eval {default_scope ->{order "#{base.quoted_table_name}.display_order ASC"}}

# Mustache
base.send :include, MustacheContext
Expand Down
2 changes: 1 addition & 1 deletion lib/surveyor/models/survey_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.included(base)
base.send :has_many, :translations, :class_name => "SurveyTranslation"

# Scopes
base.send :scope, :with_sections, {:include => :sections}
base.instance_eval {scope :with_sections, ->{includes :sections}}

@@validations_already_included ||= nil
unless @@validations_already_included
Expand Down
2 changes: 1 addition & 1 deletion lib/surveyor/models/survey_section_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.included(base)
base.send :belongs_to, :survey

# Scopes
base.send :scope, :with_includes, { :include => {:questions => [:answers, :question_group, {:dependency => :dependency_conditions}]}}
base.instance_eval {scope :with_includes, ->{ includes :questions => [:answers, :question_group, {:dependency => :dependency_conditions}]}}

@@validations_already_included ||= nil
unless @@validations_already_included
Expand Down

0 comments on commit ade06ea

Please sign in to comment.