Module: Redis::Autosuggest::SuggestRails
- Defined in:
- lib/redis/autosuggest/rails/sources.rb
Class Method Summary collapse
- .add_source_attr(record, column, options) ⇒ Object
- .add_to_autosuggest(record) ⇒ Object
- .check_if_changed(record) ⇒ Object
-
.get_size(model, attr) ⇒ Object
Get the size (how many items) of a model/attribute pair.
-
.incr_size(model, attr, incr = 1) ⇒ Object
Increment the key storing the size of a model/attribute pair.
- .init_rails_sources ⇒ Object
- .remove_from_autosuggest(record) ⇒ Object
Class Method Details
.add_source_attr(record, column, options) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 58 def add_source_attr(record, column, ) item = record.send(column) size = self.get_size(record.class, column).to_i if size < [:limit] score = record.send([:rank_by]) unless [:rank_by].nil? score ||= 0 is_new_item = Redis::Autosuggest.add_with_score(item, score) size = self.incr_size(record.class, column) if is_new_item end return size end |
.add_to_autosuggest(record) ⇒ Object
52 53 54 55 56 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 52 def add_to_autosuggest(record) Redis::Autosuggest.rails_sources[record.class].each do |column, | self.add_source_attr(record, column, ) end end |
.check_if_changed(record) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 70 def check_if_changed(record) Redis::Autosuggest.rails_sources[record.class].each_key do |column| next unless record.send("#{column}_changed?") old_item = record.send("#{column}_was") score = Redis::Autosuggest.get_score(old_item) Redis::Autosuggest.remove(old_item) Redis::Autosuggest.add_with_score(record.send(column), score) end end |
.get_size(model, attr) ⇒ Object
Get the size (how many items) of a model/attribute pair
89 90 91 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 89 def get_size(model, attr) Redis::Autosuggest.rails_source_sizes.get("#{model}:#{attr}") end |
.incr_size(model, attr, incr = 1) ⇒ Object
Increment the key storing the size of a model/attribute pair
94 95 96 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 94 def incr_size(model, attr, incr=1) return Redis::Autosuggest.rails_source_sizes.incrby("#{model}:#{attr}", incr) end |
.init_rails_sources ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 37 def init_rails_sources ::Rails.application.eager_load! # Redis::Autosuggest.db.flushdb Redis::Autosuggest.rails_sources.each do |model, attrs| attrs.each do |column, | order = [:init_order] || "" model.order(order).find_each do |record| puts "Adding #{record.send(column)}" size = self.add_source_attr(record, column, ) break if size >= [:limit] end end end end |
.remove_from_autosuggest(record) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/redis/autosuggest/rails/sources.rb', line 80 def remove_from_autosuggest(record) Redis::Autosuggest.rails_sources[record.class].each_key do |column| item = record.send(column) item_was_in_db = Redis::Autosuggest.remove(item) self.incr_size(record.class, column, -1) if item_was_in_db end end |