Sha256: 19d52d3e6a22b7959a911e84d3a1a43ecc2583a11fbe2ea8a184f7ce9c7cc961

Contents?: true

Size: 884 Bytes

Versions: 2

Compression:

Stored size: 884 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)
          object.update_attribute(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
        end
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
on_the_spot-0.0.3 lib/on_the_spot/controller_extension.rb
on_the_spot-0.0.2 lib/on_the_spot/controller_extension.rb