Class: ArFinderForm::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_finder_form/context.rb

Constant Summary collapse

FIND_OPTIONS_KEYS =
[:order, :group, :limit, :offset, :include, 
  :select, :from, :readonly, :lock
]
PAGINATE_OPTIONS_KEYS =
[:per_page, :page, :total_entries, :count, :finder]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, options = {}) ⇒ Context

Returns a new instance of Context.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ar_finder_form/context.rb', line 13

def initialize(form, options = {})
  @form, @options = form, options
  @where, @params = [], []
  @joins = []
  @connector = options.delete(:connector) || 'AND'
  FIND_OPTIONS_KEYS.each do |attr_name|
    if value = @options[attr_name]
      @find_options ||= {}
      @find_options[attr_name] = value
    end
  end
  PAGINATE_OPTIONS_KEYS.each do |attr_name|
    if value = @options[attr_name]
      @paginate_options ||= {}
      @paginate_options[attr_name] = value
    end
  end
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



9
10
11
# File 'lib/ar_finder_form/context.rb', line 9

def form
  @form
end

#joinsObject (readonly)

Returns the value of attribute joins.



9
10
11
# File 'lib/ar_finder_form/context.rb', line 9

def joins
  @joins
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/ar_finder_form/context.rb', line 9

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/ar_finder_form/context.rb', line 10

def params
  @params
end

#single_tableObject

Returns the value of attribute single_table.



11
12
13
# File 'lib/ar_finder_form/context.rb', line 11

def single_table
  @single_table
end

#whereObject (readonly)

Returns the value of attribute where.



10
11
12
# File 'lib/ar_finder_form/context.rb', line 10

def where
  @where
end

Class Method Details

.build(form, options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/ar_finder_form/context.rb', line 104

def build(form, options)
  builder = form.class.builder
  options = 
    (form.find_options || {}).dup.
    update(form.find_options || {}).
    update(options || {})
  context = Context.new(form, options)
  context.build(builder)
  context
end

Instance Method Details

#add_condition(where, *params) ⇒ Object



32
33
34
35
# File 'lib/ar_finder_form/context.rb', line 32

def add_condition(where, *params)
  @where << where
  @params.concat(params)
end

#build(builder) ⇒ Object



94
95
96
97
98
# File 'lib/ar_finder_form/context.rb', line 94

def build(builder)
  form.send(:before_build, self) if form.respond_to?(:before_build)
  builder.build(self)
  form.send(:after_build, self) if form.respond_to?(:after_build)
end

#empty?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ar_finder_form/context.rb', line 75

def empty?
  to_find_options[:conditions].nil? && joins.empty?
end

#find_optionsObject



100
# File 'lib/ar_finder_form/context.rb', line 100

def find_options; @find_options ||= {}; end

#merge(sub_context) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ar_finder_form/context.rb', line 79

def merge(sub_context)
  conditions = sub_context.to_find_options[:conditions]
  if conditions
    if conditions.is_a?(Array)
      add_condition(conditions.shift, *conditions)
    else
      add_condition(conditions)
    end
  end
  yield if block_given?
  unless sub_context.joins.empty?
    joins.concat(sub_context.joins)
  end
end

#new_sub_context(options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/ar_finder_form/context.rb', line 69

def new_sub_context(options = {})
  result = Context.new(form, options)
  result.single_table = self.single_table
  result
end

#paginate_optionsObject



101
# File 'lib/ar_finder_form/context.rb', line 101

def paginate_options; @paginate_options ||= {}; end

#single_table?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ar_finder_form/context.rb', line 65

def single_table?
  @single_table
end

#to_find_options(options = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ar_finder_form/context.rb', line 37

def to_find_options(options = nil)
  conditions = @where.join(" %s " % @connector)
  unless @params.empty?
    conditions = [conditions].concat(@params)
  end
  result = {}
  if find_options
    FIND_OPTIONS_KEYS.each do |attr_name|
      value = find_options[attr_name]
      result[attr_name] = value unless value.blank?
    end
  end
  result[:joins] = joins.join(' ') unless joins.empty?
  result[:conditions] = conditions unless conditions.empty?
  options ? result.update(options) : result
end

#to_paginate_options(options = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/ar_finder_form/context.rb', line 54

def to_paginate_options(options = nil)
  result = to_find_options(options)
  if paginate_options
    PAGINATE_OPTIONS_KEYS.each do |attr_name|
      value = paginate_options[attr_name]
      result[attr_name] = value unless value.blank?
    end
  end
  result
end