Module: Glue::Orderable
- Includes:
- Aspects
- Defined in:
- lib/glue/orderable.rb
Overview
Attach list/ordering methods to the enchanted class.
Instance Method Summary collapse
- #add_to ⇒ Object
- #add_to_bottom ⇒ Object
- #add_to_top ⇒ Object
- #bottom? ⇒ Boolean (also: #last?)
- #bottom_item ⇒ Object
- #bottom_position ⇒ Object
- #decrement_position ⇒ Object
- #decrement_position_of_lower_items ⇒ Object
- #higher_item ⇒ Object (also: #previous_item)
- #increment_position ⇒ Object
- #increment_position_of_all_items ⇒ Object
- #increment_position_of_higher_items ⇒ Object
- #lower_item ⇒ Object (also: #next_item)
-
#move_higher ⇒ Object
Move higher.
-
#move_lower ⇒ Object
Move lower.
-
#move_to(dest_position) ⇒ Object
Move to a specific position.
-
#move_to_bottom ⇒ Object
Move to the bottom.
-
#move_to_top ⇒ Object
Move to the top.
- #set_bottom_position ⇒ Object
- #set_top_position ⇒ Object
- #top? ⇒ Boolean (also: #first?)
- #top_item ⇒ Object (also: #first_item)
Instance Method Details
#add_to ⇒ Object
145 146 147 |
# File 'lib/glue/orderable.rb', line 145 def add_to # TODO end |
#add_to_bottom ⇒ Object
141 142 143 |
# File 'lib/glue/orderable.rb', line 141 def add_to_bottom @position = bottom_position + 1 end |
#add_to_top ⇒ Object
137 138 139 |
# File 'lib/glue/orderable.rb', line 137 def add_to_top increment_position_of_all_items end |
#bottom? ⇒ Boolean Also known as: last?
181 182 183 |
# File 'lib/glue/orderable.rb', line 181 def bottom? @position == bottom_position end |
#bottom_item ⇒ Object
168 169 170 171 172 173 |
# File 'lib/glue/orderable.rb', line 168 def bottom_item pos = orderable_position con = orderable_condition con = con.empty? ? nil : con.join(' AND ') self.class.one(:condition => con, :order => "#{pos} DESC", :limit => 1) end |
#bottom_position ⇒ Object
196 197 198 199 |
# File 'lib/glue/orderable.rb', line 196 def bottom_position item = bottom_item item ? item.send(orderable_position) : 0 end |
#decrement_position ⇒ Object
191 192 193 194 |
# File 'lib/glue/orderable.rb', line 191 def decrement_position @position -= 1 update_property(:"#{orderable_position}") end |
#decrement_position_of_lower_items ⇒ Object
224 225 226 227 228 |
# File 'lib/glue/orderable.rb', line 224 def decrement_position_of_lower_items pos = orderable_position con = orderable_condition + [ "#{pos} > #{@position}" ] self.class.update( "#{pos}=(#{pos} - 1)", :condition => con.join(' AND ') ) end |
#higher_item ⇒ Object Also known as: previous_item
149 150 151 152 153 |
# File 'lib/glue/orderable.rb', line 149 def higher_item pos = orderable_position con = orderable_condition + [ "#{pos} = #{@position - 1}" ] self.class.one( :condition => con.join(' AND ') ) end |
#increment_position ⇒ Object
186 187 188 189 |
# File 'lib/glue/orderable.rb', line 186 def increment_position @position += 1 update_property(:"#{orderable_position}") end |
#increment_position_of_all_items ⇒ Object
217 218 219 220 221 222 |
# File 'lib/glue/orderable.rb', line 217 def increment_position_of_all_items pos = orderable_position con = orderable_condition con = con.empty? ? nil : con.join(' AND ') self.class.update("#{pos}=(#{pos} + 1)", :condition => con ) end |
#increment_position_of_higher_items ⇒ Object
211 212 213 214 215 |
# File 'lib/glue/orderable.rb', line 211 def increment_position_of_higher_items pos = orderable_position con = orderable_condition + [ "#{pos} < #{@position}" ] self.class.update( "#{pos}=(#{pos} + 1)", :condition => con.join(' AND ') ) end |
#lower_item ⇒ Object Also known as: next_item
156 157 158 159 160 |
# File 'lib/glue/orderable.rb', line 156 def lower_item pos = orderable_position con = orderable_condition + [ "#{pos} = #{@position + 1}" ] self.class.one( :condition => con.join(' AND ') ) end |
#move_higher ⇒ Object
Move higher.
75 76 77 78 79 80 81 82 |
# File 'lib/glue/orderable.rb', line 75 def move_higher if higher = higher_item self.class.transaction do higher.increment_position decrement_position end end end |
#move_lower ⇒ Object
Move lower.
86 87 88 89 90 91 92 93 |
# File 'lib/glue/orderable.rb', line 86 def move_lower if lower = lower_item self.class.transaction do lower.decrement_position increment_position end end end |
#move_to(dest_position) ⇒ Object
Move to a specific position.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/glue/orderable.rb', line 115 def move_to(dest_position) return if @position == dest_position pos = orderable_position con = orderable_condition self.class.transaction do if @position < dest_position adj = "#{pos} = #{pos} - 1" con = con + [ "#{pos} > #{@position}", "#{pos} <= #{dest_position}" ] else adj = "#{pos} = #{pos} + 1" con = con + [ "#{pos} < #{@position}", "#{pos} >= #{dest_position}" ] end self.class.update( adj, :condition => con.join(' AND ') ) @position = dest_position update_property(:"#{pos}") end self end |
#move_to_bottom ⇒ Object
Move to the bottom.
106 107 108 109 110 111 |
# File 'lib/glue/orderable.rb', line 106 def move_to_bottom self.class.transaction do decrement_position_of_lower_items set_bottom_position end end |
#move_to_top ⇒ Object
Move to the top.
97 98 99 100 101 102 |
# File 'lib/glue/orderable.rb', line 97 def move_to_top self.class.transaction do increment_position_of_higher_items set_top_position end end |
#set_bottom_position ⇒ Object
206 207 208 209 |
# File 'lib/glue/orderable.rb', line 206 def set_bottom_position @position = bottom_position + 1 update_property(:"#{orderable_position}") end |
#set_top_position ⇒ Object
201 202 203 204 |
# File 'lib/glue/orderable.rb', line 201 def set_top_position @position = 1 update_property(:"#{orderable_position}") end |
#top? ⇒ Boolean Also known as: first?
176 177 178 |
# File 'lib/glue/orderable.rb', line 176 def top? @position == 1 end |
#top_item ⇒ Object Also known as: first_item
163 164 165 |
# File 'lib/glue/orderable.rb', line 163 def top_item # TODO end |