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



93
94
95
96
97
# File 'lib/supplejack/url_formats/item_hash.rb', line 93

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

#and_filters(filter_type = nil) ⇒ Object



69
70
71
72
# File 'lib/supplejack/url_formats/item_hash.rb', line 69

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

#filter_symbol(filter_type = nil) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/supplejack/url_formats/item_hash.rb', line 99

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



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

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



145
146
147
# File 'lib/supplejack/url_formats/item_hash.rb', line 145

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

#is_text_field?(filter) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/supplejack/url_formats/item_hash.rb', line 74

def is_text_field?(filter)
  return false if filter.nil?
  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.

Parameters:

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

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



156
157
158
159
160
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
# File 'lib/supplejack/url_formats/item_hash.rb', line 156

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



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/supplejack/url_formats/item_hash.rb', line 128

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/supplejack/url_formats/item_hash.rb', line 112

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
# 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?
  
  if params[:sort].present?
    hash[:sort] = params[:sort]
    hash[:direction] = params[:direction] || "asc"
  end
  
  hash
end

#without_filters(filter_type = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/supplejack/url_formats/item_hash.rb', line 79

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