Module: Positionable::PositionableMethods

Defined in:
lib/positionable.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#is_positionable(options = {}) ⇒ Object

Makes this model positionable.

class Item < ActiveRecord::Base
  is_positionable
end

Maybe your items are grouped (typically with a belongs_to association). In this case, you’ll want to restrict the position in each group by declaring the :scope option:

class Folder < ActiveRecord::Base
  has_many :items
end

class Item < ActiveRecord::Base
  belongs_to :folder
  is_positionable :scope => :folder
end

By default, position starts by zero. But you may want to change this at the model level, for instance by starting at one (which seems more natural for some people):

class Item < ActiveRecord::Base
  is_positionable :start => 1
end

To make new records to appear at first position, the default ordering can be changed as follows:

class Item < ActiveRecord::Base
  is_positionable :order => :desc
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/positionable.rb', line 62

def is_positionable(options = {})
  include InstanceMethods

  scope_id_attr = "#{options[:scope].to_s}_id" if options[:scope]
  start = options[:start] || 0
  order = options[:order] || :asc

  default_scope { order("#{ActiveRecord::Base.connection.quote_table_name self.table_name}.#{ActiveRecord::Base.connection.quote_column_name 'position'} #{order}") }

  before_create :add_to_bottom
  before_update :update_position
  after_destroy :decrement_all_next

  if scope_id_attr
    class_eval <<-RUBY

      def scope_id
        send(:"#{scope_id_attr}")
      end

      # Gives the range of available positions for this record, whithin the provided scope.
      # If no scope is provided, then it takes the record's current scope by default.
      # If this record is new and no scope can be retrieved, then a <tt>RangeWithoutScopeError</tt>
      # is raised.
      def range(scope = nil)
        raise RangeWithoutScopeError if new_record? and scope.nil? and scope_id.nil?
        # Does its best to retrieve the target scope...
        target_scope_id = scope.nil? ? scope_id : scope.id
        # Number of records whithin the target scope
        count = if target_scope_id.nil?
          self.class.where("#{scope_id_attr} IS NULL").count
        else
          self.class.where("#{scope_id_attr} = ?", target_scope_id).count
        end
        # An additional position is available if this record is new, or if it's moved to another scope
        if new_record? or target_scope_id != scope_id
          (start..(start + count))
        else
          (start..(start + count - 1))
        end
      end

    private

      def scoped_condition
        if scope_id.nil?
          "#{scope_id_attr} is null"
        else
          "#{scope_id_attr} = " + scope_id.to_s
        end
      end

      def scoped_position
        scoped_condition + " and position"
      end

      def scope_changed?
        send(:"#{scope_id_attr}_changed?")
      end

      def scope_id_was
        send(:"#{scope_id_attr}_was")
      end

      def scoped_condition_was
        if scope_id_was.nil?
          "#{scope_id_attr} IS NULL"
        else
          "#{scope_id_attr} = " + scope_id_was.to_s
        end
      end

      def scoped_position_was
        scoped_condition_was + " and position"
      end

    RUBY
  else
    class_eval <<-RUBY

      # Gives the range of available positions for this record.
      def range
        if new_record?
          (start..(bottom + 1))
        else
          (start..bottom)
        end
      end

    private

      def scope_changed?
        false
      end

      def scoped_condition
        ""
      end

      def scoped_position
        "position"
      end

    RUBY
  end

  class_eval <<-RUBY
    def start
      #{start}
    end
  RUBY
end