Sha256: d089d25c0c87e80613aa35ee4d6dbd066282bfddc3ae701f7d56c5736f7dcb25

Contents?: true

Size: 1014 Bytes

Versions: 13

Compression:

Stored size: 1014 Bytes

Contents

module OnTheSpot
  module ControllerExtension

    def self.included(base)
      base.extend ClassMethods
    end

    # if this method is called inside a controller, the edit-on-the-spot
    # controller action is added that will allow to edit fields in place
    module ClassMethods
      def can_edit_on_the_spot
        define_method :update_attribute_on_the_spot do
          klass, field, id = params[:id].split('__')
          select_data = params[:select_array]
          object = klass.camelize.constantize.find(id)
          if object.update_attributes(field => params[:value])
            if select_data.nil?
              render :text => CGI::escapeHTML(object.send(field).to_s)
            else
              parsed_data = JSON.parse(select_data.gsub("'", '"'))
              render :text => parsed_data[object.send(field).to_s]
            end
          else
            render :text => object.errors.full_messages.join("\n"), :status => 422
          end
        end
      end
    end
    
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
on_the_spot-0.0.17 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.16 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.14 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.13 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.12 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.11 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.10 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.9 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.8 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.7 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.6 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.5 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.4 lib/on_the_spot/controller_extension.rb