module Crudie
Crudie
spec
Crudie::Spec::Unit
usage: RSpec.describe ProjectsController, :type => :controller do
include Crudie::Spec include_crudie_spec_for :project, :context_name => :user
end
Constants
- VERSION
Public Class Methods
included(base)
click to toggle source
# File lib/crudie/controller_methods.rb, line 2 def self.included base def base.crudie resource, options = {} template_base = options[:template_base] || "" template_extension = options[:template_extension] || "json.jbuilder" resources = resource.to_s.pluralize added_methods = options[:only] || [ :create, :index, :show, :update, :destroy] excluded_methods = options[:except] || [] added_methods = added_methods - excluded_methods define_method(:template_path) do |action| "#{template_base}#{resources}/#{action}.#{template_extension}" end private :template_path # define method: `create` # create resource under `crudie_context` using `crudie_params` # render `#{template_base}#{resources}/create.#{template_extension}` if added_methods.include?(:create) define_method(:create) do instance = crudie_context.create(crudie_params) if instance.valid? instance_variable_set("@#{resource}".to_sym, instance) render :template => template_path(:create) else render :status => 409, :json => { :reason => instance.errors.messages } end end end if added_methods.include?(:index) # define method: `index` # list resources via `crudie_params` # render `#{template_base}#{resources}/index.#{template_extension}` define_method(:index) do instance_variable_set("@#{resources}".to_sym, crudie_context) render :template => template_path(:index) end end if added_methods.include?(:show) # define method: `show` # show resource via `crudie_context.find(params[:id])` # render `#{template_base}#{resources}/show.#{template_extension}` define_method(:show) do instance = crudie_context.find(params[:id]) instance_variable_set("@#{resource}".to_sym, instance) render :template => template_path(:show) end end if added_methods.include?(:update) # define method: `update` # update resource via `crudie_context.find(params[:id])` with `crudie_params` # render `#{template_base}#{resources}/update.#{template_extension}` define_method(:update) do instance = crudie_context.find(params[:id]) updating_success = instance.update_attributes(crudie_params) if updating_success instance_variable_set("@#{resource}".to_sym, instance) render :template => template_path(:update) else render :status => 409, :json => { :reason => instance.errors.messages } end end end if added_methods.include?(:destroy) # define method: `destroy` # destroy resource via `crudie_context.find(params[:id])` define_method(:destroy) do instance = crudie_context.find(params[:id]) instance.destroy render :json => { :status => 'destroy isntance ok' } end end end end