Module: IIFinder::Core

Extended by:
ActiveSupport::Concern
Includes:
Coactive::Initializer
Included in:
Base
Defined in:
lib/ii_finder/core.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve_args(finder, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/ii_finder/core.rb', line 73

def resolve_args(finder, *args)
  if args.size == 0 || args.size == 1
    model = finder.class.lookup
    raise IIFinder::Error.new("could not find model for #{finder.class}") unless model
    return model.all,  args[0] || {}
  else
    return args[0], args[1]
  end
end

Instance Method Details

#callObject



42
43
44
45
46
47
48
49
# File 'lib/ii_finder/core.rb', line 42

def call
  self.class._parameters.each do |param|
    value = fetch_criteria(param.name)
    if value.present? || param.allow_blank?
      merge_relation!(send(param.name, value))
    end
  end
end

#call_allObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ii_finder/core.rb', line 25

def call_all
  planned = case IIFinder.config.traversal
    when :preorder
      [self] + coactors
    when :postorder
      coactors + [self]
    when :inorder
      planned = coactors.in_groups(2, false)
      planned[0] + [self] + planned[1]
    end

  planned.each do |finder|
    relation = finder == self ? call : finder.call(@context)
    @context.relation = @context.relation.merge(relation) if relation.respond_to?(:merge)
  end
end

#fetch_criteria(name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/ii_finder/core.rb', line 51

def fetch_criteria(name)
  if @criteria.respond_to?(:fetch)
    @criteria.fetch(name, nil)
  elsif @criteria.respond_to?(name)
    @criteria.send(name)
  end
end

#initialize(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ii_finder/core.rb', line 14

def initialize(*args)
  if args[0].is_a?(self.class.context_class)
    super(args[0])
  else
    relation, criteria = Core.resolve_args(self, *args)
    model = relation.klass
    table = model.arel_table if model.respond_to?(:arel_table)
    super(relation: relation, criteria: criteria, model: model, table: table)
  end
end

#merge_relation!(relation) ⇒ Object



59
60
61
62
63
# File 'lib/ii_finder/core.rb', line 59

def merge_relation!(relation)
  if relation.respond_to?(:merge) && Config.merge_relation
    @relation = @relation.merge(relation)
  end
end