Project

General

Profile

« Previous | Next » 

Revision 02e4c535

Added by Daniel Lobato Garcia over 8 years ago

Fixes #11924 - Substitute .scoped by .where(nil) to force return relation

On Rails 4 .scoped is deprecated. Calling .all on the model returns the
equivalent ActiveRecord relation object on Rails 4, but on Rails 3 it
returns an Array right away.

A proper replacement we can use is where(nil) - it's ugly but it returns
the same relation in both Rails 3 and 4. There are a couple of fixes too
on models such as bookmark.rb which return where conditions ({} and
similar) instead of a relation, which is also a deprecated behavior.

We could possibly substitute these by .all after the Rails 4 migration
if they feel too 'unidiomatic'.

View differences:

app/controllers/api/base_controller.rb
def parent_scope
parent_name, scope = parent_resource_details
return resource_class.scoped unless scope
return resource_class.where(nil) unless scope
association = resource_class.reflect_on_all_associations.find {|assoc| assoc.plural_name == parent_name.pluralize}
resource_class.joins(association.name).merge(scope)
app/controllers/application_controller.rb
@resource_base ||= if model_of_controller.respond_to?(:authorized)
model_of_controller.authorized(current_permission)
else
model_of_controller.scoped
model_of_controller.where(nil)
end
end
app/controllers/filters_controller.rb
@resource_base ||= if @role.present?
@role.filters.authorized(current_permission)
else
Filter.scoped.authorized(current_permission)
Filter.where(nil).authorized(current_permission)
end
end
app/helpers/taxonomy_helper.rb
association = resource.to_s.classify.constantize
end
return unless User.current.allowed_to?("view_#{resource}".to_sym)
ids = "#{association.scoped.klass.to_s.underscore.singularize}_ids".to_sym
ids = "#{association.where(nil).klass.to_s.underscore.singularize}_ids".to_sym
content_tag(:div, :id => resource, :class => "tab-pane") do
all_checkbox(f, resource) +
multiple_selects(f, association.scoped.klass.to_s.underscore.pluralize.to_sym, association, taxonomy.selected_or_inherited_ids[ids],
multiple_selects(f, association.where(nil).klass.to_s.underscore.pluralize.to_sym, association, taxonomy.selected_or_inherited_ids[ids],
{:disabled => taxonomy.used_and_selected_or_inherited_ids[ids],
:label => translated_label(resource, :select)},
{'data-mismatches' => taxonomy.need_to_be_selected_ids[ids].to_json,
app/models/bookmark.rb
scope :my_bookmarks, lambda {
user = User.current
return {} unless SETTINGS[:login] and !user.nil?
user = User.current
conditions = sanitize_sql_for_conditions(["((bookmarks.public = ?) OR (bookmarks.owner_id = ? AND bookmarks.owner_type = 'User'))", true, user.id])
if !SETTINGS[:login] || user.nil?
conditions = {}
else
conditions = sanitize_sql_for_conditions(["((bookmarks.public = ?) OR (bookmarks.owner_id = ? AND bookmarks.owner_type = 'User'))", true, user.id])
end
where(conditions)
}
app/models/concerns/authorizable.rb
if user.nil?
self.where('1=0')
elsif user.admin?
self.scoped
self.where(nil)
else
Authorizer.new(user).find_collection(resource || self, :permission => permission)
end
app/models/concerns/host_common.rb
end
def available_puppetclasses
return Puppetclass.scoped if environment_id.blank?
return Puppetclass.where(nil) if environment_id.blank?
environment.puppetclasses - parent_classes
end
app/models/concerns/taxonomix.rb
# default scope is not called if we just use #scoped therefore we have to enforce quering
# to get correct default values
def enforce_default
self.scoped.limit(0).all unless which_ancestry_method.present?
self.where(nil).limit(0).all unless which_ancestry_method.present?
end
def taxable_ids(loc = which_location, org = which_organization, inner_method = which_ancestry_method)
app/models/config_group.rb
alias_method :individual_puppetclasses, :puppetclasses
def available_puppetclasses
Puppetclass.scoped
Puppetclass.where(nil)
end
# for auditing
app/models/hostgroup.rb
def params
parameters = {}
# read common parameters
CommonParameter.scoped.each {|p| parameters.update Hash[p.name => p.value] }
CommonParameter.where(nil).each {|p| parameters.update Hash[p.name => p.value] }
# read OS parameters
operatingsystem.os_parameters.each {|p| parameters.update Hash[p.name => p.value] } if operatingsystem
# read group parameters only if a host belongs to a group
app/models/nic/base.rb
scope :physical, -> { where(:virtual => false) }
scope :is_managed, -> { where(:managed => true) }
scope :primary, -> { { :conditions => { :primary => true } } }
scope :provision, -> { { :conditions => { :provision => true } } }
scope :primary, -> { where(:primary => true) }
scope :provision, -> { where(:provision => true) }
belongs_to :subnet
belongs_to :domain, :counter_cache => 'hosts_count'
......
private
def interface_attribute_uniqueness(attr, base = Nic::Base.scoped)
def interface_attribute_uniqueness(attr, base = Nic::Base.where(nil))
in_memory_candidates = self.host.present? ? self.host.interfaces.select { |i| i.persisted? && !i.marked_for_destruction? } : [self]
db_candidates = base.where(attr => self.public_send(attr))
db_candidates = db_candidates.select { |c| c.id != self.id && in_memory_candidates.map(&:id).include?(c.id) }
app/models/role.rb
audited :allow_mass_assignment => true
scope :givable, -> { where(:builtin => 0).order(:name) }
scope :for_current_user, -> { User.current.admin? ? {} : where(:id => User.current.role_ids) }
scope :for_current_user, -> { User.current.admin? ? where('0 != 0') : where(:id => User.current.role_ids) }
scope :builtin, lambda { |*args|
compare = 'not' if args.first
where("#{compare} builtin = 0")
db/migrate/20150521121315_rename_config_template_to_provisioning_template.rb
old_name = 'ConfigTemplate'
new_name = 'ProvisioningTemplate'
Template.update_all "type = '#{new_name}'", "type = '#{old_name}'"
Audit.update_all "auditable_type = '#{new_name}'", "auditable_type = '#{old_name}'"
TaxableTaxonomy.update_all "taxable_type = '#{new_name}'", "taxable_type = '#{old_name}'"
Permission.update_all "resource_type = '#{new_name}'", "resource_type = '#{old_name}'"
Template.where(:type => old_name).update_all(:type => new_name)
Audit.where(:auditable_type => old_name).update_all(:auditable_type => new_name)
TaxableTaxonomy.where(:taxable_type => old_name).update_all(:taxable_type => new_name)
Permission.where(:resource_type => old_name).update_all(:resource_type => new_name)
PERMISSIONS.each do |from|
to = from.sub('templates', 'provisioning_templates')
say "renaming permission #{from} to #{to}"
Permission.update_all "name = '#{to}'", "name = '#{from}'"
Permission.where(:name => from).update_all(:name => to)
end
if foreign_keys('os_default_templates').find { |f| f.options[:name] == 'os_default_templates_config_template_id_fk' }.present?
......
PERMISSIONS.each do |to|
from = to.sub('provisioning_templates', 'templates')
say "renaming permission #{from} to #{to}"
Permission.update_all "name = '#{to}'", "name = '#{from}'"
Permission.where(:name => from).update_all(:name => to)
end
old_name = 'ConfigTemplate'
new_name = 'ProvisioningTemplate'
Template.update_all "type = '#{old_name}'", "type = '#{new_name}'"
Audit.update_all "auditable_type = '#{old_name}'", "auditable_type = '#{new_name}'"
TaxableTaxonomy.update_all "taxable_type = '#{old_name}'", "taxable_type = '#{new_name}'"
Permission.update_all "resource_type = '#{old_name}'", "resource_type = '#{new_name}'"
Template.where(:type => new_name).update_all(:type => old_name)
Audit.where(:auditable_type => new_name).update_all(:auditable_type => old_name)
TaxableTaxonomy.where(:taxable_type => new_name).update_all(:taxable_type => old_name)
Permission.where(:resource_type => new_name).update_all(:resource_type => old_name)
end
end
lib/core_extensions.rb
alias_attribute :to_s, :to_label
def self.unconfigured?
scoped.reorder('').limit(1).pluck(self.base_class.primary_key).empty?
where(nil).reorder('').limit(1).pluck(self.base_class.primary_key).empty?
end
def self.per_page
test/unit/host_test.rb
test "available_puppetclasses should return all if no environment" do
host = FactoryGirl.create(:host)
host.update_attribute(:environment_id, nil)
assert_equal Puppetclass.scoped, host.available_puppetclasses
assert_equal Puppetclass.where(nil), host.available_puppetclasses
end
test "available_puppetclasses should return environment-specific classes" do
host = FactoryGirl.create(:host, :with_environment)
refute_equal Puppetclass.scoped, host.available_puppetclasses
refute_equal Puppetclass.where(nil), host.available_puppetclasses
assert_equal host.environment.puppetclasses.sort, host.available_puppetclasses.sort
end
test "available_puppetclasses should return environment-specific classes (and that are NOT already inherited by parent)" do
hostgroup = FactoryGirl.create(:hostgroup, :with_puppetclass)
host = FactoryGirl.create(:host, :hostgroup => hostgroup, :environment => hostgroup.environment)
refute_equal Puppetclass.scoped, host.available_puppetclasses
refute_equal Puppetclass.where(nil), host.available_puppetclasses
refute_equal host.environment.puppetclasses.sort, host.available_puppetclasses.sort
assert_equal (host.environment.puppetclasses - host.parent_classes).sort, host.available_puppetclasses.sort
end
test/unit/provisioning_template_test.rb
test '#preview_host_collection obeys view_hosts permission' do
provisioning_template = FactoryGirl.build(:provisioning_template)
Host.expects(:authorized).with(:view_hosts).returns(Host.scoped)
Host.expects(:authorized).with(:view_hosts).returns(Host.where(nil))
provisioning_template.preview_host_collection
end
test/unit/ptable_test.rb
test '#preview_host_collection obeys view_hosts permission' do
ptable = FactoryGirl.build(:ptable)
Host.expects(:authorized).with(:view_hosts).returns(Host.scoped)
Host.expects(:authorized).with(:view_hosts).returns(Host.where(nil))
ptable.preview_host_collection
end
end

Also available in: Unified diff