Sha256: 3e8bb8e20af29bc3c57ad8ac91673f2ff77a8eaedb8530b1171ff5ef3483403b

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

module SmarterListing::ControllerExtension

  def self.included base
    base.helper_method :collection, :resource
    base.include SmarterListing::Helper
  end

  def index
    collection
    respond_to do |format|
      format.html { render layout: self.class._layout }
      format.js { render action: 'index.js.erb' }
    end
  end

  def new
    instance_variable_get(resource_ivar) || instance_variable_set(resource_ivar, model.new(_resource_params))
    render 'smarter_listing/new'
  end

  def create
    instance_variable_get(resource_ivar) || instance_variable_set(resource_ivar, model.create(_resource_params))
    render 'smarter_listing/create'
  end

  def copy
    instance_variable_set resource_ivar, resource.dup
    render 'smarter_listing/copy'
  end

  def show
    resource
    render resource, object: resource
  end

  def edit
    resource
    render 'smarter_listing/edit'
  end

  def update
    resource.update _resource_params
    render 'smarter_listing/update'
  end

  def destroy
    resource.destroy
    render 'smarter_listing/destroy'
  end

  def filter_parameter
    self.class.instance_variable_get :@filter_parameter
  end

  def filtered(model)
    (!params[filter_parameter].blank?) ? model.search { fulltext "#{params[filter_parameter]}" }.results : model.all
  end

  def load_collection
    instance_variable_set collection_ivar, smart_listing_create(collection_sym, filtered(model), partial: "#{current_engine}/#{table_name}/table_header")
  end

  def load_resource
    instance_variable_set resource_ivar, (model.find(params[:id]) rescue (action_name == 'new' ? model.new(_resource_params) : nil))
  end

  def resource
    instance_variable_get(resource_ivar) || load_resource
  end

  def collection
    instance_variable_get(collection_ivar) || load_collection
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smarter_listing-0.2.2 lib/smarter_listing/controller_extension.rb
smarter_listing-0.2.1 lib/smarter_listing/controller_extension.rb
smarter_listing-0.2.0 lib/smarter_listing/controller_extension.rb
smarter_listing-0.1.9 lib/smarter_listing/controller_extension.rb
smarter_listing-0.1.8 lib/smarter_listing/controller_extension.rb
smarter_listing-0.1.7 lib/smarter_listing/controller_extension.rb