Class: QueryableWith::QueryableParameter
- Inherits:
-
Object
- Object
- QueryableWith::QueryableParameter
- Defined in:
- lib/queryable_with.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#column_name ⇒ Object
readonly
Returns the value of attribute column_name.
-
#expected_parameter ⇒ Object
readonly
Returns the value of attribute expected_parameter.
Instance Method Summary collapse
- #blank_allowed? ⇒ Boolean
- #has_default? ⇒ Boolean
-
#initialize(expected_parameter, options = {}, &block) ⇒ QueryableParameter
constructor
A new instance of QueryableParameter.
- #query(queryer, params = {}) ⇒ Object
- #scope_name ⇒ Object
- #scoped? ⇒ Boolean
- #wildcard? ⇒ Boolean
Constructor Details
#initialize(expected_parameter, options = {}, &block) ⇒ QueryableParameter
Returns a new instance of QueryableParameter.
123 124 125 126 127 128 |
# File 'lib/queryable_with.rb', line 123 def initialize(expected_parameter, ={}, &block) @scope, @wildcard, @default_value, @allow_blank = .values_at(:scope, :wildcard, :default, :allow_blank) @expected_parameter = expected_parameter.to_sym @column_name = [:column] || @expected_parameter.to_s @value_mapper = block || lambda {|o| o} end |
Instance Attribute Details
#column_name ⇒ Object (readonly)
Returns the value of attribute column_name.
121 122 123 |
# File 'lib/queryable_with.rb', line 121 def column_name @column_name end |
#expected_parameter ⇒ Object (readonly)
Returns the value of attribute expected_parameter.
121 122 123 |
# File 'lib/queryable_with.rb', line 121 def expected_parameter @expected_parameter end |
Instance Method Details
#blank_allowed? ⇒ Boolean
132 |
# File 'lib/queryable_with.rb', line 132 def blank_allowed?; @allow_blank == true; end |
#has_default? ⇒ Boolean
133 |
# File 'lib/queryable_with.rb', line 133 def has_default?; !@default_value.nil?; end |
#query(queryer, params = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/queryable_with.rb', line 136 def query(queryer, params={}) params = params.with_indifferent_access params_contain_queryable_value, queried_value = determine_queried_value(params[@expected_parameter]) return queryer unless params_contain_queryable_value queried_value = @value_mapper.call(queried_value) if scoped? || queryer_scoped?(queryer) queryer.send scope_name, queried_value else queryer.scoped(:conditions => conditions_for(queryer, queried_value)) end end |
#scope_name ⇒ Object
134 |
# File 'lib/queryable_with.rb', line 134 def scope_name; @scope || self.expected_parameter; end |
#scoped? ⇒ Boolean
130 |
# File 'lib/queryable_with.rb', line 130 def scoped?; !@scope.blank?; end |
#wildcard? ⇒ Boolean
131 |
# File 'lib/queryable_with.rb', line 131 def wildcard?; @wildcard == true; end |