Module: MorseControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Includes:
- CrudHelpers, FlashHelpers
- Defined in:
- lib/morse_controller_helpers.rb,
lib/morse_controller_helpers/version.rb,
lib/morse_controller_helpers/crud_helpers.rb,
lib/morse_controller_helpers/flash_helpers.rb
Defined Under Namespace
Modules: CrudHelpers, FlashHelpers
Constant Summary
collapse
- VERSION =
'0.1.13'.freeze
Instance Method Summary
collapse
#flash_404_error, #flash_create_no, #flash_create_yes, #flash_destroy_no, #flash_destroy_yes, #flash_update_no, #flash_update_yes
#create, #destroy, #edit, #index, #new, #show, #update
Instance Method Details
#assign_current_instance_from_params(pri) ⇒ Object
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/morse_controller_helpers.rb', line 21
def assign_current_instance_from_params(pri)
klass_us = pri.gsub('_id', '')
klass = klass_us.classify.constantize
if klass.respond_to?(:friendly)
instance_variable_set "@#{klass_us}", klass.friendly.find(params[pri])
else
instance_variable_set "@#{klass_us}", klass.find(params[pri])
end
@current_instance = set_current_instance(klass_us)
end
|
#current_instances ⇒ Object
32
33
34
|
# File 'lib/morse_controller_helpers.rb', line 32
def current_instances
@current_instances = klass.all
end
|
#dynamic_current_instance ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/morse_controller_helpers.rb', line 36
def dynamic_current_instance
params_resource_ids.each do |pri|
begin
if try(:dynamic_current_instance_exceptions)
next if dynamic_current_instance_exceptions.include? pri
end
assign_current_instance_from_params(pri)
rescue ActiveRecord::RecordNotFound
render_404
end
end
end
|
#klass(str = klass_camel) ⇒ Object
49
50
51
|
# File 'lib/morse_controller_helpers.rb', line 49
def klass(str = klass_camel)
str.classify.constantize
end
|
#klass_camel ⇒ Object
53
54
55
|
# File 'lib/morse_controller_helpers.rb', line 53
def klass_camel
controller_name.singularize
end
|
#klass_humanized ⇒ Object
57
58
59
|
# File 'lib/morse_controller_helpers.rb', line 57
def klass_humanized
klass_camel.humanize
end
|
#klass_id ⇒ Object
61
62
63
|
# File 'lib/morse_controller_helpers.rb', line 61
def klass_id
"#{klass_snake}_id"
end
|
#klass_pluralized ⇒ Object
65
66
67
|
# File 'lib/morse_controller_helpers.rb', line 65
def klass_pluralized
klass_snake.pluralize
end
|
#klass_snake(str = klass_camel) ⇒ Object
69
70
71
|
# File 'lib/morse_controller_helpers.rb', line 69
def klass_snake(str = klass_camel)
str.underscore
end
|
#params_resource ⇒ Object
81
82
83
|
# File 'lib/morse_controller_helpers.rb', line 81
def params_resource
params.require(resource_symbol).permit!
end
|
#params_resource_ids ⇒ Object
73
74
75
|
# File 'lib/morse_controller_helpers.rb', line 73
def params_resource_ids
params.keys.collect { |p| p if p.include?('_id') }.compact
end
|
#params_resources ⇒ Object
77
78
79
|
# File 'lib/morse_controller_helpers.rb', line 77
def params_resources
params_resource_ids.map { |p| p.gsub('_id', '') }
end
|
#path_edit(cp = current_instance) ⇒ Object
85
86
87
|
# File 'lib/morse_controller_helpers.rb', line 85
def path_edit(cp = current_instance)
[:edit, path_prefix, cp].compact
end
|
#path_index ⇒ Object
89
90
91
|
# File 'lib/morse_controller_helpers.rb', line 89
def path_index
[path_prefix, resource_symbols].compact
end
|
#path_new ⇒ Object
93
94
95
|
# File 'lib/morse_controller_helpers.rb', line 93
def path_new
[:new, path_prefix, resource_symbol].compact
end
|
#path_prefix ⇒ Object
97
98
99
|
# File 'lib/morse_controller_helpers.rb', line 97
def path_prefix
nil
end
|
#path_prefix_symbol ⇒ Object
101
102
103
104
|
# File 'lib/morse_controller_helpers.rb', line 101
def path_prefix_symbol
return nil unless path_prefix
path_prefix.to_sym
end
|
#path_show(instance = current_instance) ⇒ Object
106
107
108
|
# File 'lib/morse_controller_helpers.rb', line 106
def path_show(instance = current_instance)
[path_prefix, instance].compact
end
|
#render_404 ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/morse_controller_helpers.rb', line 118
def render_404
flash_404_error
respond_to do |format|
format.html do
render file: "#{Rails.root}/public/404",
layout: false,
status: :not_found
end
format.xml { head :not_found }
format.any { head :not_found }
end
end
|
#resource_symbol ⇒ Object
110
111
112
|
# File 'lib/morse_controller_helpers.rb', line 110
def resource_symbol
klass_snake.to_sym
end
|
#set_current_instance(k) ⇒ Object
131
132
133
|
# File 'lib/morse_controller_helpers.rb', line 131
def set_current_instance(k)
instance_variable_get("@#{k}") unless action_name == 'index'
end
|