Module: ActiveCrudify::ClassMethods
- Defined in:
- lib/active_crudify/class_methods.rb
Instance Method Summary collapse
-
#crudify(model_name, options = {}) ⇒ Object
Parameters * model_name Specify the model name like :post, :blog * options.
Instance Method Details
#crudify(model_name, options = {}) ⇒ Object
Parameters
-
model_name Specify the model name like :post, :blog
-
options
Options
- :class_name
-
Specify the class name for model.
- :plural_name
- :order_by
- :conditions
- :order_by
-
Specify the condition which will be used in where clauses.
- :namespace
-
Specify the controller in which namespace, like admin.
- :only
-
Actions will be defined for controller.
- :except
-
Define the actions exclude this option specified.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/active_crudify/class_methods.rb', line 21 def crudify(model_name, = {}) self.responder = ActiveCrudify::Responder respond_to :html, :json, :xml = ActiveCrudify.(model_name).merge() allowed_actions = [:index, :new, :create, :show, :edit, :update, :destroy] if [:only] allowed_actions = [:only] elsif [:except] allowed_actions = allowed_actions - [:except] end singular_name = [:singular_name] plural_name = [:plural_name] class_name = [:class_name] klass = class_name.constantize namespace = [:namespace] responded = namespace ? [namespace] : [] [:paginate] = [:paginate] && klass.respond_to?(:page) module_eval %( protected def set_crud_options @crud_options ||= #{.inspect} end def what @what ||= '#{[:title]}' end def find_#{singular_name} set_instance(#{class_name}.find(params[:id])) end def paginate_all_#{plural_name} set_collection(#{class_name}.page(params[:page]).order(@crud_options[:order_by]).where(conditions)) end def find_all_#{plural_name} set_collection(#{class_name}.order(@crud_options[:order_by]).where(conditions)) end def conditions @conditions ||= @crud_options[:conditions] end def set_instance(record) @instance = @#{singular_name} = record end def set_collection(records) @collection = @#{plural_name} = records end ) module_eval %( before_filter :find_#{singular_name}, :only => [:update, :destroy, :edit, :show] before_filter :set_crud_options if #{allowed_actions.include?(:new)} def new @#{singular_name} = #{class_name}.new end end if #{allowed_actions.include?(:show)} def show respond_with(@#{singular_name}) end end if #{allowed_actions.include?(:create)} def create @instance = @#{singular_name} = #{class_name}.new(params[:#{singular_name}]) return if before_create === false @instance.save respond_with(#{namespace.inspect}, @#{singular_name}) end end if #{allowed_actions.include?(:edit)} def edit end end if #{allowed_actions.include?(:update)} def update return if before_update === false @#{singular_name}.update_attributes(params[:#{singular_name}]) respond_with(#{namespace.inspect}, @#{singular_name}) end end if #{allowed_actions.include?(:destroy)} def destroy return if before_destroy === false @#{singular_name}.destroy respond_to do |f| f.html { redirect_to([#{namespace.inspect}, :#{plural_name}]) } f.any(:xml, :json) {head :ok} end end end ) if allowed_actions.include?(:index) if [:paginate] module_eval %( def index paginate_all_#{plural_name} respond_with(#{responded} << @#{plural_name}) end ) else module_eval %( def index find_all_#{plural_name} respond_with(#{responded} << @#{plural_name}) end ) end end end |