Module: OpenStax::Utilities::ActsAsNumberable::ContainerInstanceMethods
- Defined in:
- lib/openstax/utilities/acts_as_numberable.rb
Instance Method Summary collapse
- #me_and_peers ⇒ Object
- #move_to_container!(new_container) ⇒ Object
- #my_class ⇒ Object
- #remove_from_container! ⇒ Object
Instance Method Details
#me_and_peers ⇒ Object
197 198 199 |
# File 'lib/openstax/utilities/acts_as_numberable.rb', line 197 def me_and_peers my_class.where(container_column => self.send(container_column)) end |
#move_to_container!(new_container) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/openstax/utilities/acts_as_numberable.rb', line 159 def move_to_container!(new_container) return if new_container.id == self.send(container_column) ActiveRecord::Base.transaction do remove_from_container! self.send container + "=", new_container self.assign_number self.save! self.changed_sets = true end end |
#my_class ⇒ Object
193 194 195 |
# File 'lib/openstax/utilities/acts_as_numberable.rb', line 193 def my_class table_class || self.class end |
#remove_from_container! ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/openstax/utilities/acts_as_numberable.rb', line 172 def remove_from_container! later_items = my_class.where(container_column => self.send(container_column)) .where("#{number_field} > ?", self.send(number_field)) if !self.destroyed self.send "#{number_field}=", nil self.send container_column + '=', nil self.save! end # Do this to make sure that the reordering below doesn't # cause a number to be duplicated temporarily (which would # cause a validation error) later_items.sort_by!{|item| item.send(number_field)} later_items.each do |later| later.send("#{number_field}=", later.send(number_field)-1) later.save! end end |