module CRUDable::Controller

Public Instance Methods

create() click to toggle source
# File lib/crudable/controller.rb, line 38
def create
  instance_variable_set("@#{klass_singular_variable}", klass.new(send("#{klass_singular_variable}_params")))

  respond_to do |format|
    if instance_variable_get("@#{klass_singular_variable}").save
      format.html { redirect_to({ action: :index }) }
      format.json { render json: instance_variable_get("@#{klass_singular_variable}"), status: :created, location: instance_variable_get("@#{klass_singular_variable}") }
      format.xml  { render xml: instance_variable_get("@#{klass_singular_variable}"), status: :created, location: instance_variable_get("@#{klass_singular_variable}") }
    else
      format.html { render 'form' }
      format.json { render json: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
      format.json { render xml: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
    end
  end
end
destroy() click to toggle source
# File lib/crudable/controller.rb, line 80
def destroy
  instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))

  if instance_variable_get("@#{klass_singular_variable}").destroy
    flash[:notice] = 'Successfully deleted.'
  else
    flash[:error] = 'Delete failed.'
  end

  respond_to do |format|
    if instance_variable_get("@#{klass_singular_variable}").destroy
      format.html { redirect_to(:back) }
      format.json { head :ok }
      format.xml  { head :ok }
    else

    end
  end
end
edit() click to toggle source
# File lib/crudable/controller.rb, line 54
def edit
  instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))

  respond_to do |format|
    format.html { render 'form' }
    format.json { render json: instance_variable_get("@#{klass_singular_variable}") }
    format.xml  { render xml: instance_variable_get("@#{klass_singular_variable}") }
  end
end
index() click to toggle source
# File lib/crudable/controller.rb, line 8
def index
  instance_variable_set("@#{klass.table_name}", klass.all)

  respond_to do |format|
    format.html
    format.json { render json: instance_variable_get("@#{klass.table_name}")  }
    format.xml  { render xml: instance_variable_get("@#{klass.table_name}") }
  end
end
new() click to toggle source
# File lib/crudable/controller.rb, line 28
def new
  instance_variable_set("@#{klass_singular_variable}", klass.new)

  respond_to do |format|
    format.html { render 'form' }
    format.json { render json: instance_variable_get("@#{klass_singular_variable}") }
    format.xml  { render xml: instance_variable_get("@#{klass_singular_variable}") }
  end
end
show() click to toggle source
# File lib/crudable/controller.rb, line 18
def show
  instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))

  respond_to do |format|
    format.html
    format.json { render json: instance_variable_get("@#{klass_singular_variable}") }
    format.xml  { render xml: instance_variable_get("@#{klass_singular_variable}") }
  end
end
update() click to toggle source
# File lib/crudable/controller.rb, line 64
def update
  instance_variable_set("@#{klass_singular_variable}", klass.find(params[:id]))

  respond_to do |format|
    if instance_variable_get("@#{klass_singular_variable}").update(send("#{klass_singular_variable}_params"))
      format.html { redirect_to({ action: :index }) }
      format.json { head :ok }
      format.xml  { head :ok }
    else
      format.html { render 'form' }
      format.json { render json: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
      format.json { render xml: instance_variable_get("@#{klass_singular_variable}").errors, status: :unprocessable_entity }
    end
  end
end

Private Instance Methods

klass() click to toggle source
# File lib/crudable/controller.rb, line 102
def klass
  controller_name.classify.constantize
end
klass_singular_variable() click to toggle source
# File lib/crudable/controller.rb, line 106
def klass_singular_variable
  klass.to_s.downcase.freeze
end