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
# 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("`#{self.table_name}`.`position` #{order}")

  before_create :add_to_bottom
  before_update :update_position
  after_destroy :decrement_all_next

  if scope_id_attr
    class_eval "\n      def scope_id\n        send(:\"\#{scope_id_attr}\")\n      end\n\n      # Gives the range of available positions for this record, whithin the provided scope.\n      # If no scope is provided, then it takes the record's current scope by default.\n      # If this record is new and no scope can be retrieved, then a <tt>RangeWithoutScopeError</tt>\n      # is raised.\n      def range(scope = nil)\n        raise RangeWithoutScopeError if new_record? and scope.nil? and scope_id.nil?\n        # Does its best to retrieve the target scope...\n        target_scope_id = scope.nil? ? scope_id : scope.id\n        # Number of records whithin the target scope\n        count = self.class.where(\"\#{scope_id_attr} = ?\", target_scope_id).count\n        # An additional position is available if this record is new, or if it's moved to another scope\n        if new_record? or target_scope_id != scope_id\n          (start..(start + count))\n        else\n          (start..(start + count - 1))\n        end\n      end\n\n    private\n\n      def scoped_condition\n        if scope_id.nil?\n          \"\#{scope_id_attr} is null\"\n        else\n          \"\#{scope_id_attr} = \" + scope_id.to_s\n        end\n      end\n\n      def scoped_position\n        scoped_condition + \" and position\"\n      end\n\n      def scope_changed?\n        send(:\"\#{scope_id_attr}_changed?\")\n      end\n\n      def scope_id_was\n        send(:\"\#{scope_id_attr}_was\")\n      end\n\n      def scoped_condition_was\n        \"\#{scope_id_attr} = \" + scope_id_was.to_s\n      end\n\n      def scoped_position_was\n        scoped_condition_was + \" and position\"\n      end\n\n    RUBY\n  else\n    class_eval <<-RUBY\n\n      # Gives the range of available positions for this record.\n      def range\n        if new_record?\n          (start..(bottom + 1))\n        else\n          (start..bottom)\n        end\n      end\n\n    private\n\n      def scope_changed?\n        false\n      end\n\n      def scoped_condition\n        \"\"\n      end\n\n      def scoped_position\n        \"position\"\n      end\n\n    RUBY\n  end\n\n  class_eval <<-RUBY\n    def start\n      \#{start}\n    end\n  RUBY\nend\n"