module MinimumCrud::Controllers::Base

Public Instance Methods

create() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 62
def create
  @record = @model.new(record_params)

  respond_to do |format|
    if @record.save
      format.html { redirect_to ({action: :show, id: @record}),
                    notice: message_on_create(@record) }
      if self.enable_json
        format.json { render :show, status: :created, location: @record }
      end
    else
      format.html { render sub_layout_path(:new) }
      if self.enable_json
        format.json { render json: @record.errors, status: :unprocessable_entity }
      end
    end
  end
end
destroy() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 98
def destroy
  @record.destroy
  respond_to do |format|
    format.html { redirect_to ({action: :index}),
                  notice: message_on_destroy(@record) }
    if self.enable_json
      format.json { head :no_content }
    end
  end
end
edit() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 58
def edit
  render sub_layout_path
end
index() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 34
def index
  @records = @model.all
  respond_to do |format|
    format.html { render sub_layout_path }
    if self.enable_json
      format.json { }
    end
  end
end
minimum_crud_enable_json(is_enable) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 26
def minimum_crud_enable_json(is_enable)
  self.enable_json = is_enable
end
minimum_crud_permit_params(*permit_params) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 29
def minimum_crud_permit_params(*permit_params)
  self.permit_params = permit_params
end
minimum_crud_sub_layout(name) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 23
def minimum_crud_sub_layout(name)
  self.sub_layout = name
end
new() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 53
def new
  @record = @model.new
  render sub_layout_path
end
show() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 44
def show
  respond_to do |format|
    format.html { render sub_layout_path }
    if self.enable_json
      format.json { }
    end
  end
end
sub_layout_path(action = nil) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 109
def sub_layout_path(action = nil)
  action ||= action_name
  if self.class.sub_layout.try(:to_sym) == :none
    action
  else
    "layouts/minimum_crud/#{self.class.sub_layout}/#{action}"
  end
end
update() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 81
def update
  respond_to do |format|
    if @record.update(record_params)
      format.html { redirect_to ({action: :show, id: @record}),
                    notice: message_on_update(@record) }
      if self.enable_json
        format.json { render :show, status: :ok, location: @record }
      end
    else
      format.html { render sub_layout_path(:edit) }
      if self.enable_json
        format.json { render json: @record.errors, status: :unprocessable_entity }
      end
    end
  end
end

Private Instance Methods

message_on_create(_record) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 130
def message_on_create(_record)
  I18n.t('helpers.created', model: @model.model_name.human)
end
message_on_destroy(_record) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 136
def message_on_destroy(_record)
  I18n.t('helpers.deleted', model: @model.model_name.human)
end
message_on_update(_record) click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 133
def message_on_update(_record)
  I18n.t('helpers.updated', model: @model.model_name.human)
end
record_params() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 125
def record_params
  params.require(@model.model_name.name.underscore)
        .permit(self.permit_params)
end
set_model() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 119
def set_model
  @model = self.class.to_s.gsub(/Controller\Z/, '').classify.constantize
end
set_record() click to toggle source
# File lib/minimum_crud/controllers/base.rb, line 122
def set_record
  @record = @model.find(params[:id])
end