Sha256: 127fdb1ac73d3127faca8aa4de35ea33cedaa59ad78a459cbf1ca227a9e8f5e2

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

class FamilyGallery::ResourcesController < FamilyGallery::BaseController
  # Set load_and_authorize for classes that extend this one.
  def self.inherited(subclass)
    name_match = subclass.name.match(/\AFamilyGallery::(.+)sController\Z/)
    class_name = "FamilyGallery::#{name_match[1]}"
    subclass.load_and_authorize_resource class_name: class_name

    subclass.__send__(:define_method, :resource_name) do
      name_match[1]
    end
  end

  def index
    @ransack = resource_class.ransack(params[:q])
  end

  def new
    assign_params_to_resource_instance
  end

  def create
    assign_params_to_resource_instance

    if resource_instance.save
      flash[:notice] = t(".resource_was_saved")
      redirect_to resource_instance
    else
      render :new
    end
  end

  def show
  end

  def edit
    assign_params_to_resource_instance
  end

  def update
    assign_params_to_resource_instance

    if resource_instance.save
      flash[:notice] = t(".resource_was_updated")
      redirect_to resource_instance
    else
      render :edit
    end
  end

  def destroy
    if resource_instance.destroy
      flash[:notice] = t(".resource_was_deleted")
      redirect_to root_url
    else
      flash[:error] = resource_instance.errors.full_messages.join(". ")
      redirect_to resource_instance
    end
  end

private

  def resource_class
    FamilyGallery.const_get(resource_name)
  end

  def resource_instance
    unless @resource_instance
      @resource_instance = instance_variable_get("@#{StringCases.camel_to_snake(resource_name)}")
    end

    @resource_instance
  end

  def resource_extracted_params
    key_name = StringCases.camel_to_snake(resource_name)
    return params[key_name]
  end

  def assign_params_to_resource_instance
    resource_instance.assign_attributes(resource_params) if resource_extracted_params

    begin
      after_assign
    rescue NameError
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
family_gallery-0.0.3 app/controllers/family_gallery/resources_controller.rb
family_gallery-0.0.2 app/controllers/family_gallery/resources_controller.rb
family_gallery-0.0.1 app/controllers/family_gallery/resources_controller.rb