Class: ArFinderForm::Context
- Inherits:
-
Object
- Object
- ArFinderForm::Context
- 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
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#single_table ⇒ Object
Returns the value of attribute single_table.
-
#where ⇒ Object
readonly
Returns the value of attribute where.
Class Method Summary collapse
Instance Method Summary collapse
- #add_condition(where, *params) ⇒ Object
- #build(builder) ⇒ Object
- #empty? ⇒ Boolean
- #find_options ⇒ Object
-
#initialize(form, options = {}) ⇒ Context
constructor
A new instance of Context.
- #merge(sub_context) ⇒ Object
- #new_sub_context(options = {}) ⇒ Object
- #paginate_options ⇒ Object
- #single_table? ⇒ Boolean
- #to_find_options(options = nil) ⇒ Object
- #to_paginate_options(options = nil) ⇒ Object
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, = {}) @form, @options = form, @where, @params = [], [] @joins = [] @connector = .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
#form ⇒ Object (readonly)
Returns the value of attribute form.
9 10 11 |
# File 'lib/ar_finder_form/context.rb', line 9 def form @form end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
9 10 11 |
# File 'lib/ar_finder_form/context.rb', line 9 def joins @joins end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/ar_finder_form/context.rb', line 9 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/ar_finder_form/context.rb', line 10 def params @params end |
#single_table ⇒ Object
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 |
#where ⇒ Object (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
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
75 76 77 |
# File 'lib/ar_finder_form/context.rb', line 75 def empty? [:conditions].nil? && joins.empty? end |
#find_options ⇒ Object
100 |
# File 'lib/ar_finder_form/context.rb', line 100 def ; @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.[: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( = {}) result = Context.new(form, ) result.single_table = self.single_table result end |
#paginate_options ⇒ Object
101 |
# File 'lib/ar_finder_form/context.rb', line 101 def ; @paginate_options ||= {}; end |
#single_table? ⇒ 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 ( = nil) conditions = @where.join(" %s " % @connector) unless @params.empty? conditions = [conditions].concat(@params) end result = {} if FIND_OPTIONS_KEYS.each do |attr_name| value = [attr_name] result[attr_name] = value unless value.blank? end end result[:joins] = joins.join(' ') unless joins.empty? result[:conditions] = conditions unless conditions.empty? ? result.update() : 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 ( = nil) result = () if PAGINATE_OPTIONS_KEYS.each do |attr_name| value = [attr_name] result[attr_name] = value unless value.blank? end end result end |