Class: RankedModel::Ranker::Mapper
- Inherits:
-
Object
- Object
- RankedModel::Ranker::Mapper
- Defined in:
- lib/ranked-model/ranker.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#ranker ⇒ Object
Returns the value of attribute ranker.
Instance Method Summary collapse
- #current_at_position(_pos) ⇒ Object
- #handle_ranking ⇒ Object
- #has_rank? ⇒ Boolean
-
#initialize(ranker, instance) ⇒ Mapper
constructor
A new instance of Mapper.
- #position ⇒ Object
- #rank ⇒ Object
- #relative_rank ⇒ Object
- #reset_ranks! ⇒ Object
- #update_rank!(value) ⇒ Object
- #validate_ranker_for_instance! ⇒ Object
Constructor Details
#initialize(ranker, instance) ⇒ Mapper
Returns a new instance of Mapper.
26 27 28 29 30 31 |
# File 'lib/ranked-model/ranker.rb', line 26 def initialize ranker, instance self.ranker = ranker self.instance = instance validate_ranker_for_instance! end |
Instance Attribute Details
#instance ⇒ Object
Returns the value of attribute instance.
24 25 26 |
# File 'lib/ranked-model/ranker.rb', line 24 def instance @instance end |
#ranker ⇒ Object
Returns the value of attribute ranker.
24 25 26 |
# File 'lib/ranked-model/ranker.rb', line 24 def ranker @ranker end |
Instance Method Details
#current_at_position(_pos) ⇒ Object
90 91 92 93 94 |
# File 'lib/ranked-model/ranker.rb', line 90 def current_at_position _pos if (ordered_instance = finder.offset(_pos).first) RankedModel::Ranker::Mapper.new ranker, ordered_instance end end |
#handle_ranking ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ranked-model/ranker.rb', line 52 def handle_ranking case ranker.unless when Proc return if ranker.unless.call(instance) when Symbol return if instance.send(ranker.unless) end update_index_from_position assure_unique_position end |
#has_rank? ⇒ Boolean
96 97 98 |
# File 'lib/ranked-model/ranker.rb', line 96 def has_rank? !rank.nil? end |
#position ⇒ Object
76 77 78 |
# File 'lib/ranked-model/ranker.rb', line 76 def position instance.send "#{ranker.name}_position" end |
#rank ⇒ Object
86 87 88 |
# File 'lib/ranked-model/ranker.rb', line 86 def rank instance.send "#{ranker.column}" end |
#relative_rank ⇒ Object
80 81 82 83 84 |
# File 'lib/ranked-model/ranker.rb', line 80 def relative_rank escaped_column = instance_class.connection.quote_column_name ranker.column finder.where("#{escaped_column} < #{rank}").count(:all) end |
#reset_ranks! ⇒ Object
72 73 74 |
# File 'lib/ranked-model/ranker.rb', line 72 def reset_ranks! finder.update_all(ranker.column => nil) end |
#update_rank!(value) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/ranked-model/ranker.rb', line 64 def update_rank! value # Bypass callbacks # instance_class. where(instance_class.primary_key => instance.id). update_all(ranker.column => value) end |
#validate_ranker_for_instance! ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ranked-model/ranker.rb', line 33 def validate_ranker_for_instance! if ranker.scope && !instance_class.respond_to?(ranker.scope) raise RankedModel::InvalidScope, %Q{No scope called "#{ranker.scope}" found in model} end if ranker.with_same if (case ranker.with_same when Symbol !instance.respond_to?(ranker.with_same) when Array array_element = ranker.with_same.detect {|attr| !instance.respond_to?(attr) } else false end) raise RankedModel::InvalidField, %Q{No field called "#{array_element || ranker.with_same}" found in model} end end end |