Module: IsPositionable::ClassMethods

Defined in:
lib/is_positionable.rb

Instance Method Summary collapse

Instance Method Details

#is_positionable(options = {}) ⇒ Object

Is Positionable



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/is_positionable.rb', line 14

def is_positionable(options = {})
  
  # Initialize the interface builer class
  # Passes in the options hash and the controller_name and builds the methods for it
  # Interface will convert ugly multi-dot-syntax to a single object.method wrapper
  interface = IsPositionable::Interface.new(options, controller_name).build

  # ControllerAction
  
  # Requires the acts_as_list gem
  # Ignores "MissingSourceFile" error so that a "nice" error message can be
  # displayed in the browser.
  interface.require_gem('acts_as_list')

  # Initializes Acts As List on the model
  # Will set up the column, the same that is used by Is Positionable
  # The Acts As List Gem will be provided with the same scope as the Is Positionable Gem uses
  # The default_scope will be set to always sort the items by the specified column (default = :position)
  interface.model.class_eval do
    
    # Initializes the Acts As List gem
    # Sets the :column to the specified column attribute, default => :position
    # Sets the :scope attribute to the specified attribute, default => '1 = 1' (like Acts As List)
    # Will display a helpful error when the Acts As List Gem has either not been installed or loaded
    begin
      acts_as_list  :column => interface.column,
                    :scope  => interface.scope || '1 = 1'
    rescue NoMethodError
      raise IsPositionable::Errors::NoMethodError::ActsAsList.new
    end
    
    # Sets the default scope to order by the specified column
    # default_scope :order => interface.column if interface.set_default_scope?
            
  end
  
  # Defines the action which will be used to move objects around
  define_method(interface.action_name) do
    
    # Finds the object that will be re-positioned    
    object = interface.find(params[:id])
    
    # Move Object Up 1 Position
    if params[interface.param].eql?('higher')
      object.move_higher
    end
    
    # Move Object Down 1 Position
    if params[interface.param].eql?('lower')
      object.move_lower
    end
    
    # Move Object To Top Of List
    if params[interface.param].eql?('to_top')
      object.move_to_top
    end
    
    # Move Object To Bottom Of List
    if params[interface.param].eql?('to_bottom')
      object.move_to_bottom
    end
    
    # Redirects if this is a simple HTML POST request
    # Will do nothing if this is an AJAX request
    respond_to do |format|
      format.html do
        
        # If the redirect path has not been set, it will default to :back.
        # If the default is set, but there is no "Referer", it will by default redirect
        # the user to the index action of the current controller.
        # If the :redirect_to attribute was manually set, this will be used.
        # If the :redirect_to attribute was set to :back, it will not change the default attribute
        if interface.redirect.eql?(:back)
          if request.headers["Referer"]
            redirect_to(interface.redirect)
          else
            redirect_to(:action => :index) 
          end
        else
          redirect_to(interface.redirect) 
        end
      end
      
      # If the positioning action was triggered by a AJAX call
      # then do nothing. Only optionally render the RJS file if present.
      format.js do
        # => Nothing
      end
    end
  end # => define_method(interface.action_name)
  
  
  
  # HelperMethods

  # Include the ActionView::Helpers inside of ActionController::Base
  # This will enable html button parsing
  ActionController::Base.send(:include, ActionView::Helpers)

  # Injects the "UP" button for the view helpers into the controller
  # This will be available to all views within the specified controller
  define_method "up_button_for_#{interface.controller_name}" do |objects, object, *options|
  
    # Set default options and overwrite the existing ones with
    # possible user input
    options = {
      :name             => "up",
      :attribute        => :id,
      :url              => {
        :controller     => interface.controller_name,
        :action         => interface.action_name,
        :id             => object,
        interface.param => "higher" },
      :html             => {
        :id             => "up_button_for_#{interface.controller_name.singularize}_#{object.id}",
        :class          => "up_button_for_#{interface.controller_name}" }
    }.update(options.empty? ? {} : options.first)
    
    button_to(options[:name], options[:url], options[:html]) unless objects.first.eql?(object)
  end
  
  # Injects the "DOWN" button for the view helpers into the controller
  # This will be available to all views within the specified controller
  define_method "down_button_for_#{interface.controller_name}" do |objects, object, *options|
  
    # Set default options and overwrite the existing ones with
    # possible user input
    options = {
      :name             => "down",
      :attribute        => :id,
      :url              => {
        :controller     => interface.controller_name,
        :action         => interface.action_name,
        :id             => object,
        interface.param => "lower" },
      :html             => {
        :id             => "down_button_for_#{interface.controller_name.singularize}_#{object.id}",
        :class          => "down_button_for_#{interface.controller_name}" }
    }.update(options.empty? ? {} : options.first)
    
    button_to(options[:name], options[:url], options[:html]) unless objects.last.eql?(object)
  end

  # Injects the "TOP" button for the view helpers into the controller
  # This will be available to all views within the specified controller
  define_method "top_button_for_#{interface.controller_name}" do |objects, object, *options|
  
    # Set default options and overwrite the existing ones with
    # possible user input
    options = {
      :name             => "top",
      :attribute        => :id,
      :url              => {
        :controller     => interface.controller_name,
        :action         => interface.action_name,
        :id             => object,
        interface.param => "to_top" },
      :html             => {
        :id             => "top_button_for_#{interface.controller_name.singularize}_#{object.id}",
        :class          => "top_button_for_#{interface.controller_name}" }
    }.update(options.empty? ? {} : options.first)
  
    button_to(options[:name], options[:url], options[:html]) unless objects.first.eql?(object)
  end
       
  # Injects the "BOTTOM" button for the view helpers into the controller
  # This will be available to all views within the specified controller
  define_method "bottom_button_for_#{interface.controller_name}" do |objects, object, *options|
  
    # Set default options and overwrite the existing ones with
    # possible user input
    options = {
      :name             => "bottom",
      :attribute        => :id,
      :url              => {
        :controller     => interface.controller_name,
        :action         => interface.action_name,
        :id             => object,
        interface.param => "to_bottom" },
      :html             => {
        :id             => "bottom_button_for_#{interface.controller_name.singularize}_#{object.id}",
        :class          => "bottom_button_for_#{interface.controller_name}" }
    }.update(options.empty? ? {} : options.first)

    button_to(options[:name], options[:url], options[:html]) unless objects.last.eql?(object)
  end
  
  # Makes the buttons available to the views that belong to the
  # controller that Is Positional was invoked from
  helper_method "up_button_for_#{interface.controller_name}",
                "down_button_for_#{interface.controller_name}",
                "top_button_for_#{interface.controller_name}",
                "bottom_button_for_#{interface.controller_name}"
end