Last active
September 28, 2024 05:41
-
-
Save VishalTaj/676fc080f3c40298059dc2b23eaf5f36 to your computer and use it in GitHub Desktop.
Revisions
-
VishalTaj renamed this gist
Nov 6, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
VishalTaj revised this gist
Nov 6, 2018 . 2 changed files with 99 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 # This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
VishalTaj created this gist
Jul 15, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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