Class: DryInk::Base
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- DryInk::Base
- Defined in:
- lib/dryink/base.rb
Instance Method Summary collapse
- #child_of(classname) ⇒ Object
-
#create ⇒ Object
POST /model.
-
#destroy ⇒ Object
DELETE /model/:id.
- #discover_model ⇒ Object
-
#edit ⇒ Object
GET /model/:id/edit.
- #field(name, options = {}) ⇒ Object
- #find_object ⇒ Object
- #find_objects ⇒ Object
-
#index ⇒ Object
GET /model.
- #index_path ⇒ Object
- #instantiate_object ⇒ Object
-
#new ⇒ Object
GET /model/new.
- #new_object ⇒ Object
- #page ⇒ Object
- #per_page ⇒ Object
-
#show ⇒ Object
GET /model/:id.
-
#update ⇒ Object
PUT /model/:id.
Instance Method Details
#child_of(classname) ⇒ Object
16 17 18 19 20 |
# File 'lib/dryink/base.rb', line 16 def child_of(classname) @parent_class = Kernel.const_get(classname.to_s.camelcase) @parent_key = "#{classname}_id".to_sym @parent_object = @parent_class.find(params[@parent_key]) end |
#create ⇒ Object
POST /model
103 104 105 106 107 108 109 110 |
# File 'lib/dryink/base.rb', line 103 def create @object.attributes = params[@model_class.to_s.underscore.to_sym] if @object.save redirect_to index_path, :notice => "#{@model_singular} Created!" else render 'dryink/template/new' end end |
#destroy ⇒ Object
DELETE /model/:id
127 128 129 130 131 132 133 134 |
# File 'lib/dryink/base.rb', line 127 def destroy if @object.destroy flash[:notice] = "#{@model_singular} Deleted!" else flash[:alert] = "Unable to delete #{@model_singular}!" end redirect_to index_path end |
#discover_model ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/dryink/base.rb', line 8 def discover_model @model_class ||= Kernel.const_get(params[:controller].to_s.singularize.camelcase) @model_singular ||= @model_class.to_s.humanize @model_plural ||= @model_class.to_s.pluralize.humanize @model_symbol_singular ||= @model_class.to_s.underscore.to_sym @model_symbol_plural ||= @model_class.to_s.pluralize.underscore.to_sym end |
#edit ⇒ Object
GET /model/:id/edit
113 114 115 |
# File 'lib/dryink/base.rb', line 113 def edit render 'dryink/template/edit' end |
#field(name, options = {}) ⇒ Object
22 23 24 25 26 |
# File 'lib/dryink/base.rb', line 22 def field(name, ={}) @fields ||= Hash.new @fields[name.to_sym] = @fields[name.to_sym][:label] ||= name.to_s.humanize end |
#find_object ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/dryink/base.rb', line 48 def find_object if @parent_object @parent_object.send(@model_symbol_plural).find(params[:id]) else @model_class.find(params[:id]) end end |
#find_objects ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/dryink/base.rb', line 56 def find_objects if @parent_object @skip_pagination = true @objects = @parent_object.send(@model_symbol_plural).all else @objects = @model_class.all.page(page).per(per_page) end end |
#index ⇒ Object
GET /model
86 87 88 89 90 |
# File 'lib/dryink/base.rb', line 86 def index @objects = find_objects @page_title = @model_plural render 'dryink/template/index' end |
#index_path ⇒ Object
28 29 30 |
# File 'lib/dryink/base.rb', line 28 def index_path @parent_object ? [@parent_object, @model_symbol_plural] : [@model_symbol_plural] end |
#instantiate_object ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dryink/base.rb', line 65 def instantiate_object current_model = params[:controller].to_s.singularize.camelize current_action = params[:action].to_sym case current_action when :new, :create current_action, @model_class @object = new_object @page_title = "New #{@model_singular}" when :show, :edit, :update, :destroy @object = find_object @object_name = @object.respond_to?(:title) ? @object.title : @object.id current_action, @object @page_title = "#{@object_name} | #{@model_plural}" else @object = nil end end |
#new ⇒ Object
GET /model/new
98 99 100 |
# File 'lib/dryink/base.rb', line 98 def new render 'dryink/template/new' end |
#new_object ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/dryink/base.rb', line 40 def new_object if @parent_object @parent_object.send(@model_symbol_plural).build else @model_class.new end end |
#page ⇒ Object
32 33 34 |
# File 'lib/dryink/base.rb', line 32 def page params[:page] || 1 end |
#per_page ⇒ Object
36 37 38 |
# File 'lib/dryink/base.rb', line 36 def per_page 25 end |
#show ⇒ Object
GET /model/:id
93 94 95 |
# File 'lib/dryink/base.rb', line 93 def show render 'dryink/template/show' end |
#update ⇒ Object
PUT /model/:id
118 119 120 121 122 123 124 |
# File 'lib/dryink/base.rb', line 118 def update if @object.update_attributes(params[@model_class.to_s.underscore.to_sym]) redirect_to index_path, :notice => "#{@model_singular} Updated!" else render 'dryink/template/edit' end end |