Class: SearchScope::FilterScope

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, label, options = {}, &block) ⇒ FilterScope

Returns a new instance of FilterScope.

[View source]

31
32
33
34
35
36
37
38
39
# File 'lib/search_scope.rb', line 31

def initialize(name, label, options={}, &block)
  label ||= name.to_s.titleize
  @name, @label = name, label
  @filter_options = HashWithIndifferentAccess.new
  @filter_option_keys = []
  @omit_from_ui = true if options.delete(:omit_from_ui)
  #eval the block so that filter options can be initialized. Should only have calls to filter_option
  instance_eval(&block)
end

Instance Attribute Details

#filter_option_keysObject (readonly)

Returns the value of attribute filter_option_keys.


30
31
32
# File 'lib/search_scope.rb', line 30

def filter_option_keys
  @filter_option_keys
end

#filter_optionsObject (readonly)

Returns the value of attribute filter_options.


30
31
32
# File 'lib/search_scope.rb', line 30

def filter_options
  @filter_options
end

#labelObject (readonly)

Returns the value of attribute label.


30
31
32
# File 'lib/search_scope.rb', line 30

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.


30
31
32
# File 'lib/search_scope.rb', line 30

def name
  @name
end

Instance Method Details

#filter_option(name, options = {}) ⇒ Object

[View source]

45
46
47
48
49
50
51
# File 'lib/search_scope.rb', line 45

def filter_option(name, options={})
  name = name.to_s
  raise "Already defined a filter_option (#{name.inspect}) for filter_scope (#{self.name})}" if filter_option_keys.include? name
  @filter_options_ordered = nil
  filter_option_keys << name
  filter_options[name] = FilterScopeOption.new(name, options[:value], options[:label], options[:scope])
end

#filter_options_orderedObject

[View source]

53
54
55
56
57
58
59
60
# File 'lib/search_scope.rb', line 53

def filter_options_ordered
  return @filter_options_ordered if @filter_options_ordered
  @filter_options_ordered = []
  filter_option_keys.each do |key|
    @filter_options_ordered << filter_options[key]
  end
  @filter_options_ordered
end

#keyObject

[View source]

62
63
64
# File 'lib/search_scope.rb', line 62

def key
  @key ||= "filter_#{name}".intern
end

#omit_from_ui?Boolean

Returns:

  • (Boolean)
[View source]

41
42
43
# File 'lib/search_scope.rb', line 41

def omit_from_ui?
  @omit_from_ui ? true : false
end

#params_keyObject

[View source]

66
67
68
# File 'lib/search_scope.rb', line 66

def params_key
  @params_key ||= key.to_s
end

#selected_option_for(params) ⇒ Object

[View source]

70
71
72
73
# File 'lib/search_scope.rb', line 70

def selected_option_for(params)
  selected_option_key = params[self.params_key]
  self.filter_options[selected_option_key]
end