Class: DryInk::Base

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/dryink/base.rb

Instance Method Summary collapse

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

#createObject

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

#destroyObject

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_modelObject



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

#editObject

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, options={})
  @fields ||= Hash.new
  @fields[name.to_sym] = options
  @fields[name.to_sym][:label] ||= name.to_s.humanize
end

#find_objectObject



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_objectsObject



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

#indexObject

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_pathObject



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_objectObject



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
    authorize! 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
    authorize! current_action, @object
    @page_title = "#{@object_name} | #{@model_plural}"
  else
    @object = nil
  end

end

#newObject

GET /model/new



98
99
100
# File 'lib/dryink/base.rb', line 98

def new
  render 'dryink/template/new'
end

#new_objectObject



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

#pageObject



32
33
34
# File 'lib/dryink/base.rb', line 32

def page
  params[:page] || 1
end

#per_pageObject



36
37
38
# File 'lib/dryink/base.rb', line 36

def per_page
  25
end

#showObject

GET /model/:id



93
94
95
# File 'lib/dryink/base.rb', line 93

def show
render 'dryink/template/show'
end

#updateObject

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