Skip to content

Instantly share code, notes, and snippets.

@VishalTaj
Last active September 28, 2024 05:41
Show Gist options
  • Select an option

  • Save VishalTaj/676fc080f3c40298059dc2b23eaf5f36 to your computer and use it in GitHub Desktop.

Select an option

Save VishalTaj/676fc080f3c40298059dc2b23eaf5f36 to your computer and use it in GitHub Desktop.

Revisions

  1. VishalTaj renamed this gist Nov 6, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. VishalTaj revised this gist Nov 6, 2018. 2 changed files with 99 additions and 0 deletions.
    1 change: 1 addition & 0 deletions crud_concern.rb → crud_concern_rails4.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    module CrudConcern
    extend ActiveSupport::Concern
    # Rails version < 5

    ##################################################################
    # This module take cares the CRUD controller methods #
    98 changes: 98 additions & 0 deletions crud_concern_rails5.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    module CrudConcern
    extend ActiveSupport::Concern
    # rails version >= 5

    ##################################################################
    # This module take cares the CRUD controller methods #
    # #
    # Note: add skip_before_action if you want to ignore any of the #
    # above action to be loaded from module #
    # additional feature has confirm verification before deleting a #
    # record. #
    # #
    ##################################################################

    included do
    before_action :init_resource
    before_action :load_resources, only: [:index, :filter]
    before_action :load_resource, only: [:new, :edit, :update, :destroy, :create, :show]
    end

    def index
    end

    def filter
    render 'index'
    end

    def new
    end

    def show
    end

    def create
    if instance_variable_get("@#{@resource}").save(send("#{@resource}_params"))
    flash[:success] = "Created a #{@resource.camelize} successfully"
    redirect_to send("#{@resources}_path")
    else
    flash[:alert] = instance_variable_get("@#{@resource}").errors.full_messages.first
    redirect_back fallback_location: send("#{@resources}_path")
    end
    end

    def edit
    end

    def update
    if instance_variable_get("@#{@resource}").update!(send("#{@resource}_params"))
    flash[:success] = "Updated a #{@resource.camelize} successfully"
    end
    redirect_back fallback_location: send("edit_#{@resource}_path", instance_variable_get("@#{@resource}"))
    end

    def destroy
    respond_to do |format|
    format.html {
    if current_user.valid_password?(authenticate_params[:password])
    instance_variable_get("@#{@resource}").destroy
    flash[:success] = "Deleted a #{@resource.camelize} successfully"
    else
    flash[:error] = 'Invalid Password'
    end
    redirect_to send("#{@resources}_path")
    }
    format.js {
    @path = send("#{@resource}_path", instance_variable_get("@#{@resource}"))
    render 'shared/delete'
    }
    end
    end

    private

    def init_resource
    @resources = controller_name
    @resource = @resources.singularize
    end

    def load_resource
    if params[:action] == "create"
    instance_variable_set "@#{@resource}", @resources.classify.constantize.new(send("#{@resource}_params"))
    else
    instance_variable_set "@#{@resource}", @resources.classify.constantize.find_or_initialize_by(id: params[:id])
    end
    end

    def load_resources
    if params[:action] == 'filter'
    instance_variable_set "@#{@resources}", @resources.classify.constantize.unscoped.filter(send("#{@resource}_filter_params")).page(params[:page])
    else
    instance_variable_set "@#{@resources}", @resources.classify.constantize.unscoped.all.page(params[:page])
    end
    end

    def authenticate_params
    params.require(:authenticate).permit(:password)
    end
    end
  3. VishalTaj created this gist Jul 15, 2018.
    62 changes: 62 additions & 0 deletions crud_concern.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    module CrudConcern
    extend ActiveSupport::Concern

    ##################################################################
    # This module take cares the CRUD controller methods #
    # #
    # Note: add skip_before_action if you want to ignore any of the #
    # above action to be loaded from module #
    ##################################################################

    included do
    before_action :init_resource
    before_action :load_resources, only: [:index]
    before_action :load_resource, only: [:new, :edit, :update, :delete, :create, :show]
    end

    def index
    end

    def new
    end

    def show
    end

    def create
    instance_variable_get("@#{@resource}").save(send("#{@resource}_params"))
    redirect_to send("#{@resources}_path")
    end

    def edit
    end

    def update
    instance_variable_get("@#{@resource}").update_attributes(send("#{@resource}_params"))
    redirect_to send("#{@resources}_path")
    end

    def destroy
    instance_variable_get("@#{@resource}").destroy
    redirect_to send("#{@resources}_path")
    end

    private

    def init_resource
    @resources = controller_name
    @resource = @resources.singularize
    end

    def load_resource
    if params[:action] == "create"
    instance_variable_set "@#{@resource}", @resources.classify.constantize.new(send("#{@resource}_params"))
    else
    instance_variable_set "@#{@resource}", @resources.classify.constantize.find_or_initialize_by(id: params[:id])
    end
    end

    def load_resources
    instance_variable_set "@#{@resources}", @resources.classify.constantize.all.page(params[:page])
    end
    end