Class: Supplejack::UrlFormats::ItemHash

Inherits:
Object
  • Object
show all
Defined in:
lib/supplejack/url_formats/item_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, search = nil) ⇒ ItemHash

Returns a new instance of ItemHash.



14
15
16
17
18
19
20
21
# File 'lib/supplejack/url_formats/item_hash.rb', line 14

def initialize(params={}, search=nil)
  @params     = params || {}
  @search     = search
  @i_unlocked = filters_of_type(:i)
  @i_locked   = filters_of_type(:il)
  @h_unlocked = filters_of_type(:h)
  @h_locked   = filters_of_type(:hl)
end

Instance Attribute Details

#h_lockedObject

Returns the value of attribute h_locked.



12
13
14
# File 'lib/supplejack/url_formats/item_hash.rb', line 12

def h_locked
  @h_locked
end

#h_unlockedObject

Returns the value of attribute h_unlocked.



12
13
14
# File 'lib/supplejack/url_formats/item_hash.rb', line 12

def h_unlocked
  @h_unlocked
end

#i_lockedObject

Returns the value of attribute i_locked.



12
13
14
# File 'lib/supplejack/url_formats/item_hash.rb', line 12

def i_locked
  @i_locked
end

#i_unlockedObject

Returns the value of attribute i_unlocked.



12
13
14
# File 'lib/supplejack/url_formats/item_hash.rb', line 12

def i_unlocked
  @i_unlocked
end

#paramsObject

Returns the value of attribute params.



12
13
14
# File 'lib/supplejack/url_formats/item_hash.rb', line 12

def params
  @params
end

#searchObject

Returns the value of attribute search.



12
13
14
# File 'lib/supplejack/url_formats/item_hash.rb', line 12

def search
  @search
end

Instance Method Details

#all_filtersObject



96
97
98
99
100
# File 'lib/supplejack/url_formats/item_hash.rb', line 96

def all_filters
  return @all_filters if @all_filters
  self.filters
  @all_filters
end

#and_filters(filter_type = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/supplejack/url_formats/item_hash.rb', line 70

def and_filters(filter_type=nil)
  @and_filters ||= {}
  valid_filters = filters(filter_type).reject {|filter, value| filter.to_s.match(/-(.+)/)}
                                      .reject {|filter, value| is_text_field?(filter)}
  @and_filters[filter_symbol(filter_type)] ||= valid_filters
end

#filter_symbol(filter_type = nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/supplejack/url_formats/item_hash.rb', line 102

def filter_symbol(filter_type=nil)
  if filter_type
    filter_type == :items ? 'i' : 'h'
  else
    params[:record_type].to_i == 0 ? 'i' : 'h'
  end
end

#filters(filter_type = nil) ⇒ Object

Returns all the active filters for the current search These filters are used to scope the search results



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/supplejack/url_formats/item_hash.rb', line 52

def filters(filter_type=nil)
  filters = {}

  symbol = filter_symbol(filter_type)

  memoized_filters = self.instance_variable_get("@#{symbol}_filters")
  return memoized_filters if memoized_filters

  unlocked = filters_of_type(symbol.to_sym)
  locked = filters_of_type("#{symbol}l".to_sym)

  filters = Supplejack::Util.deep_merge!(unlocked, locked)

  @all_filters = filters.dup.symbolize_keys.to_hash rescue {}
  self.instance_variable_set("@#{symbol}_filters", @all_filters)
  @all_filters
end

#filters_of_type(filter_type) ⇒ Object

Returns one type of filters

Parameters:

  • filter_type (:i, :il, :h, :hl)

    The symbol of the filter type



148
149
150
# File 'lib/supplejack/url_formats/item_hash.rb', line 148

def filters_of_type(filter_type)
  params[filter_type].dup.symbolize_keys.to_hash rescue {}
end

#is_text_field?(filter) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/supplejack/url_formats/item_hash.rb', line 77

def is_text_field?(filter)
  return false if filter.nil? || Supplejack.non_text_fields.include?(filter.to_sym)
  filter.to_s.split(//).last(5).join('').to_s == '_text'
end

#options(filter_options = {}) ⇒ Object

Returns a hash options to be used for generating URL’s with all the search state.

rubocop:disable Metrics/MethodLength FIXME: make me smaller!

Parameters:

  • filter_options (Hash) (defaults to: {})

    A hash of options to be able to remove or add filters



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/supplejack/url_formats/item_hash.rb', line 161

def options(filter_options={})
  filter_options.reverse_merge!({:except => [], :plus => {}})
  filter_options[:except] ||= []

  hash = {}
  {:i => :i_unlocked, :il => :i_locked, :h => :h_unlocked, :hl => :h_locked}.each_pair do |symbol, instance_name|
    filters = self.send(instance_name).clone || {}

    filters.each_pair do |name, value|
      filters.delete(name) if value.blank?
    end

    filter_options[:except].each do |exception|
      if exception.is_a?(Hash)
        facet, values_to_delete = exception.first
        values_to_delete = Util.array(values_to_delete)
        existing_values = Util.array(filters[facet])
        new_values = existing_values - values_to_delete

        if new_values.any?
          new_values = new_values.first if new_values.size == 1
          filters[facet] = new_values
        else
          filters.delete(facet)
        end
      else
        filters.delete(exception)
      end
    end

    if filter_options[:plus].try(:any?) && filter_options[:plus][symbol].try(:any?)
      filters = Util.deep_merge(filters, filter_options[:plus][symbol])
    end

    hash[symbol] = filters.symbolize_keys if filters.any?
  end

  [:text, :direction, :sort].each do |attribute|
    attribute_value = search.send(attribute)
    hash.merge!(attribute => attribute_value) if attribute_value.present?
  end

  unless filter_options[:except].include?(:page)
    hash.merge!(:page => search.page) if search.page.present? && search.page != 1
  end

  hash.merge!(:record_type => 1) if search.record_type > 0
  return hash
end

#query_fieldsObject

Returns the query_fields from the current search filters so that specific fields can be searched. The ‘_text’ is removed from the end of the field name



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/supplejack/url_formats/item_hash.rb', line 131

def query_fields
  query_fields = []

  all_filters.each do |filter, value|
    if is_text_field?(filter)
      query_fields << filter.to_s.chomp!('_text').to_sym
    end
  end

  return nil if query_fields.empty?
  query_fields
end

#text(default_text = nil) ⇒ Object

Returns the value from the text param and joins any values from fields which end in _text (ie. creator_text)

Parameters:

  • default_text (String) (defaults to: nil)

    A string with the user query



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/supplejack/url_formats/item_hash.rb', line 115

def text(default_text=nil)
  text_values = []
  text_values << default_text if default_text.present?

  all_filters.each do |filter, value|
    text_values << value if is_text_field?(filter)
  end

  return nil if text_values.empty?
  text_values.join(' ')
end

#to_api_hashObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/supplejack/url_formats/item_hash.rb', line 23

def to_api_hash
  hash = {}
  text_value = text(params[:text])
  hash[:text]             = text_value if text_value
  hash[:geo_bbox]         = params[:geo_bbox] if params[:geo_bbox]
  hash[:record_type]      = params[:record_type] || 0
  hash[:record_type]      = hash[:record_type].to_i unless hash[:record_type]=="all"
  hash[:page]             = (params[:page] || 1).to_i
  hash[:per_page]         = (params[:per_page] || Supplejack.per_page).to_i
  hash[:and]              = and_filters if and_filters.try(:any?)
  hash[:without]          = without_filters if without_filters.try(:any?)
  hash[:facets]           = params[:facets] if params[:facets].present?
  hash[:facets_per_page]  = params[:facets_per_page].to_i if params[:facets_per_page].present?
  hash[:fields]           = params[:fields] || Supplejack.fields.join(',')
  hash[:query_fields]     = query_fields if query_fields
  hash[:solr_query]       = params[:solr_query] if params[:solr_query].present?
  hash[:ignore_metrics]   = params[:ignore_metrics] if params[:ignore_metrics].present?

  if params[:sort].present?
    hash[:sort] = params[:sort]
    hash[:direction] = params[:direction] || "asc"
  end

  hash
end

#without_filters(filter_type = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/supplejack/url_formats/item_hash.rb', line 82

def without_filters(filter_type=nil)
  symbol = filter_symbol(filter_type)
  @without_filters ||= {}
  return @without_filters[symbol] if @without_filters[symbol]

  @without_filters[symbol] = {}
  filters(filter_type).each_pair do |filter, value|
    if filter.to_s.match(/-(.+)/)
      @without_filters[symbol][$1.to_sym] = value
    end
  end
  @without_filters[symbol]
end