Class: MagicScopes::AssociationScopesGenerator

Inherits:
ScopesGenerator::Base show all
Defined in:
lib/magic_scopes/scopes_generators/association.rb

Instance Method Summary collapse

Methods inherited from ScopesGenerator::Base

instance

Constructor Details

#initialize(model, attr) ⇒ AssociationScopesGenerator

Returns a new instance of AssociationScopesGenerator.



4
5
6
7
8
9
10
# File 'lib/magic_scopes/scopes_generators/association.rb', line 4

def initialize(model, attr)
  super
  @model    = model
  @attr     = attr
  @key      = "#{model.table_name}.#{attr.to_s.foreign_key}"
  @type_key = "#{model.table_name}.#{attr}_type"
end

Instance Method Details

#for(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/magic_scopes/scopes_generators/association.rb', line 12

def for(name)
  if @model.reflections[@attr].options[:polymorphic]
    scope name || "for_#{@attr}", ->(*vals) {
      raise ArgumentError, "No argument for for_#{@attr} scope" if vals.empty?
      ids_and_types = vals.map { |v| extract_ids_and_types(v, @attr) }.flatten
      conditions = ids_and_types.map { |hsh| "(#{@key} = ? AND #{@type_key} = ?)" }.join(' OR ')
      where(conditions, *ids_and_types.map(&:values).flatten)
    }
  else
    scope name || "for_#{@attr}", ->(*vals) {
      raise ArgumentError, "No argument for for_#{@attr} scope" if vals.empty?
      where(@key => vals.map { |v| extract_ids(v, @attr) }.flatten )
    }
  end
end

#not_for(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/magic_scopes/scopes_generators/association.rb', line 28

def not_for(name)
  if @model.reflections[@attr.to_sym].options[:polymorphic]
    scope name || "not_for_#{@attr}", ->(*vals) {
      raise ArgumentError, "No argument for for_#{@attr} scope" if vals.empty?
      ids_and_types = vals.map { |v| extract_ids_and_types(v, @attr) }.flatten
      conditions = ids_and_types.map { |hsh| "(#{@key} != ? AND #{@type_key} != ?)" }.join(' AND ')
      where("#{conditions} OR (#{@key} IS NULL OR #{@type_key} IS NULL)", *ids_and_types.map(&:values).flatten)
    }
  else
    scope name || "not_for_#{@attr}", ->(*vals) {
      raise ArgumentError, "No argument for for_#{@attr} scope" if vals.empty?
      ids = vals.map { |v| extract_ids(v, @attr) }.flatten
      conditions = ids.size == 1 ? "!= ?" : "NOT IN (?)"
      where("#{@key} #{conditions} OR #{@key} IS NULL", ids.size == 1 ? ids[0] : ids)
    }
  end
end