Class: Happy::Extras::ActiveModelResourceController
Constant Summary
Constants inherited
from Controller
Controller::CASCADING_SETTINGS
Instance Attribute Summary
Attributes inherited from Controller
#env, #processed_path, #unprocessed_path
#output_buffer
Instance Method Summary
collapse
Methods inherited from Controller
#app, #current_url, helpers, #initialize, #params, #request, #response, #session
#localize, #translate
#capture_template_block, #concat_output, #render, #render_resource, #render_template, #with_output_buffer
#escape_html, #html_tag, #html_tag_attributes, #link_to, #preserve, #url_for
#permissions
#set, #settings
#handle_request
#cache_control, #content_type, #halt!, #header, #layout, #max_age, #only_if_path_matches, #redirect!, #run, #serve!
#on, #path_to_regexp
Instance Method Details
#create ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 61
def create
require_permission! :create
set_singular_variable resource_with_permission_scope(:create).new(params[settings[:singular_name]], :as => settings[:role])
if singular_variable.save
redirect! singular_variable
else
render_resource_template 'new'
end
end
|
#edit ⇒ Object
72
73
74
75
76
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 72
def edit
require_permission! :edit
set_singular_variable resource_with_permission_scope(:edit).find(params['id'])
render_resource_template 'edit'
end
|
#index ⇒ Object
43
44
45
46
47
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 43
def index
require_permission! :index
set_plural_variable resource_with_permission_scope(:index).all
render_resource_template 'index'
end
|
#new ⇒ Object
55
56
57
58
59
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 55
def new
require_permission! :new
set_singular_variable resource_with_permission_scope(:new).new(params[settings[:singular_name]], :as => settings[:role])
render_resource_template 'new'
end
|
#plural_variable ⇒ Object
31
32
33
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 31
def plural_variable
instance_variable_get "@#{settings[:plural_name]}"
end
|
#render_resource_template(name) ⇒ Object
11
12
13
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 11
def render_resource_template(name)
render "#{settings[:plural_name]}/#{name}.html.haml"
end
|
#require_permission!(*args) ⇒ Object
23
24
25
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 23
def require_permission!(*args)
raise "not allowed" unless permissions.can?(*args, settings[:class])
end
|
#resource ⇒ Object
15
16
17
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 15
def resource
settings[:class]
end
|
#resource_with_permission_scope(*args) ⇒ Object
19
20
21
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 19
def resource_with_permission_scope(*args)
permissions.scoped_model(*args, settings[:class])
end
|
#root_url ⇒ Object
7
8
9
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 7
def root_url
super(settings[:plural_name])
end
|
#route ⇒ Object
90
91
92
93
94
95
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 90
def route
settings[:singular_name] ||= settings[:class].to_s.tableize.singularize
settings[:plural_name] ||= settings[:class].to_s.tableize.pluralize
super
end
|
#set_plural_variable(v) ⇒ Object
27
28
29
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 27
def set_plural_variable(v)
instance_variable_set "@#{settings[:plural_name]}", v
end
|
#set_singular_variable(v) ⇒ Object
35
36
37
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 35
def set_singular_variable(v)
instance_variable_set "@#{settings[:singular_name]}", v
end
|
#show ⇒ Object
49
50
51
52
53
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 49
def show
require_permission! :show
set_singular_variable resource_with_permission_scope(:show).find(params['id'])
render_resource_template 'show'
end
|
#singular_variable ⇒ Object
39
40
41
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 39
def singular_variable
instance_variable_get "@#{settings[:singular_name]}"
end
|
#update ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/happy/extras/active_model_resource_controller.rb', line 78
def update
require_permission! :update
set_singular_variable resource_with_permission_scope(:update).find(params['id'])
singular_variable.assign_attributes params[settings[:singular_name]], :as => settings[:role]
if singular_variable.save
redirect! singular_variable
else
render_resource_template 'edit'
end
end
|