class SimpleCrud::BaseController

Attributes

model_klass[RW]

Public Class Methods

crud_for(klass) click to toggle source
# File lib/simple_crud/base_controller.rb, line 15
def crud_for(klass)
  @model_klass = klass
end
default_crud() click to toggle source
# File lib/simple_crud/base_controller.rb, line 19
def default_crud
  matches = self.to_s.match /(?<name>.*)Controller/
  klass = matches[:name].singularize.constantize
  crud_for(klass)
end

Public Instance Methods

create() click to toggle source

POST /models POST /models.json

# File lib/simple_crud/base_controller.rb, line 52
def create
  model_set model_klass.new(model_params)

  respond_to do |wants|
    result = model.save
    call_hook :after_save, result
    if result
      flash[:notice] = t 'messages.record_created', model: t("models.#{model_name}")
      wants.html { redirect_to(model) }
      wants.json  { render :json => model, :status => :created, :location => model }
    else
      wants.html { render :action => "new" }
      wants.json  { render :json => model.errors, :status => :unprocessable_entity }
    end
  end
end
destroy() click to toggle source

DELETE /models/1 DELETE /models/1.json

# File lib/simple_crud/base_controller.rb, line 88
def destroy
  result = model.destroy
  call_hook :after_destroy, result
  flash[:notice] = t 'messages.record_destroyed', model: t("models.#{model_name}")

  respond_to do |wants|
    wants.html { redirect_to(models_path) }
    wants.json  { head :ok }
  end
end
edit() click to toggle source

GET /models/1/edit

# File lib/simple_crud/base_controller.rb, line 47
def edit
end
index() click to toggle source

GET /models GET /models.json

# File lib/simple_crud/base_controller.rb, line 28
def index
  models_set model_klass.all
  respond_with models
end
new() click to toggle source

GET /models/new GET /models/new.json

# File lib/simple_crud/base_controller.rb, line 41
def new
  model_set model_klass.new
  respond_with model
end
show() click to toggle source

GET /models/1 GET /models/1.json

# File lib/simple_crud/base_controller.rb, line 35
def show
  respond_with model
end
update() click to toggle source

PUT /models/1 PUT /models/1.json

# File lib/simple_crud/base_controller.rb, line 71
def update
  respond_to do |wants|
    result = model.update_attributes(model_params)
    call_hook :after_update_attributes, result
    if result
      flash[:notice] = t 'messages.record_updated', model: t("models.#{model_name}")
      wants.html { redirect_to(model) }
      wants.json  { head :ok }
    else
      wants.html { render :action => "edit" }
      wants.json  { render :json => model.errors, :status => :unprocessable_entity }
    end
  end
end

Private Instance Methods

call_hook(method, *args) click to toggle source
# File lib/simple_crud/base_controller.rb, line 105
def call_hook(method, *args)
  send(method, *args) if respond_to? method
end
find_model() click to toggle source
# File lib/simple_crud/base_controller.rb, line 101
def find_model
  model_set model_klass.find(params[:id])
end