Class: QueryableWith::QueryableParameter

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={}, &block)
  @scope, @wildcard, @default_value, @allow_blank = options.values_at(:scope, :wildcard, :default, :allow_blank)
  @expected_parameter = expected_parameter.to_sym
  @column_name = options[:column] || @expected_parameter.to_s
  @value_mapper = block || lambda {|o| o}
end

Instance Attribute Details

#column_nameObject (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_parameterObject (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

Returns:

  • (Boolean)


132
# File 'lib/queryable_with.rb', line 132

def blank_allowed?; @allow_blank == true; end

#has_default?Boolean

Returns:

  • (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_nameObject



134
# File 'lib/queryable_with.rb', line 134

def scope_name; @scope || self.expected_parameter; end

#scoped?Boolean

Returns:

  • (Boolean)


130
# File 'lib/queryable_with.rb', line 130

def scoped?; !@scope.blank?; end

#wildcard?Boolean

Returns:

  • (Boolean)


131
# File 'lib/queryable_with.rb', line 131

def wildcard?; @wildcard == true; end