Class: ActiveKit::Position::Positioner
- Inherits:
-
Object
- Object
- ActiveKit::Position::Positioner
- Defined in:
- lib/active_kit/position/positioner.rb
Instance Method Summary collapse
-
#initialize(record:, name:, scope:) ⇒ Positioner
constructor
A new instance of Positioner.
- #position_in_database ⇒ Object
- #position_maximum ⇒ Object
- #position_options ⇒ Object
- #reharmonize? ⇒ Boolean
- #reharmonized! ⇒ Object
- #spot_for(position:, position_maximum_cached:) ⇒ Object
Constructor Details
#initialize(record:, name:, scope:) ⇒ Positioner
Returns a new instance of Positioner.
4 5 6 7 8 9 10 11 |
# File 'lib/active_kit/position/positioner.rb', line 4 def initialize(record:, name:, scope:) @record = record @name = name @scoped_class = @record.class.where(scope).reorder("#{@name}": :asc) @reharmonize = false @positioning = Positioning.new end |
Instance Method Details
#position_in_database ⇒ Object
13 14 15 |
# File 'lib/active_kit/position/positioner.rb', line 13 def position_in_database @scoped_class.where("#{@name}": ..@record.public_send("#{@name}_in_database")).count if @record.public_send("#{@name}_in_database") end |
#position_maximum ⇒ Object
21 22 23 24 |
# File 'lib/active_kit/position/positioner.rb', line 21 def position_maximum value = self.maxivalue value ? @scoped_class.where("#{@name}": ..value).count : 0 end |
#position_options ⇒ Object
17 18 19 |
# File 'lib/active_kit/position/positioner.rb', line 17 def (1..position_maximum).map { |position| [position, "Position #{position}"] } end |
#reharmonize? ⇒ Boolean
48 49 50 |
# File 'lib/active_kit/position/positioner.rb', line 48 def reharmonize? @reharmonize end |
#reharmonized! ⇒ Object
52 53 54 |
# File 'lib/active_kit/position/positioner.rb', line 52 def reharmonized! @reharmonize = false end |
#spot_for(position:, position_maximum_cached:) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_kit/position/positioner.rb', line 26 def spot_for(position:, position_maximum_cached:) raise "position or position_maximum_cached cannot be empty for spot_for in activekit." unless position && position_maximum_cached edge_position = position_maximum_cached + 1 if position == edge_position && position == 1 value, @reharmonize = @positioning.chair_first elsif position == edge_position value, @reharmonize = @positioning.chair_below(currvalue: self.maxivalue) elsif position_in_database.nil? value, @reharmonize = @positioning.stool_above(currvalue: currvalue(position, position_maximum_cached), prevvalue: prevvalue(position, position_maximum_cached)) elsif position > position_in_database value, @reharmonize = @positioning.stool_below(currvalue: currvalue(position, position_maximum_cached), nextvalue: nextvalue(position, position_maximum_cached)) else value, @reharmonize = @positioning.stool_above(currvalue: currvalue(position, position_maximum_cached), prevvalue: prevvalue(position, position_maximum_cached)) end value end |