templet_rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +1059 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/templet/controller/USAGE +13 -0
- data/lib/generators/templet/controller/controller_generator.rb +106 -0
- data/lib/generators/templet/core/USAGE +23 -0
- data/lib/generators/templet/core/core_generator.rb +128 -0
- data/lib/generators/templet/core_rspec/USAGE +16 -0
- data/lib/generators/templet/core_rspec/core_rspec_generator.rb +65 -0
- data/lib/generators/templet/destroy/USAGE +19 -0
- data/lib/generators/templet/destroy/destroy_generator.rb +126 -0
- data/lib/generators/templet/routes/USAGE +14 -0
- data/lib/generators/templet/routes/routes_generator.rb +64 -0
- data/lib/generators/templet/rspec/USAGE +10 -0
- data/lib/generators/templet/rspec/rspec_generator.rb +124 -0
- data/lib/generators/templet/scaffold/USAGE +22 -0
- data/lib/generators/templet/scaffold/scaffold_generator.rb +75 -0
- data/lib/generators/templet/shared/actions_option.rb +24 -0
- data/lib/generators/templet/shared/add_routes_option.rb +19 -0
- data/lib/generators/templet/shared/child_option.rb +23 -0
- data/lib/generators/templet/shared/comment_tests_option.rb +19 -0
- data/lib/generators/templet/shared/core_helpers.rb +48 -0
- data/lib/generators/templet/shared/grand_parent_option.rb +20 -0
- data/lib/generators/templet/shared/model_fields.rb +55 -0
- data/lib/generators/templet/shared/model_option.rb +25 -0
- data/lib/generators/templet/shared/parent_option.rb +20 -0
- data/lib/generators/templet/templates/controller.rb.erb +108 -0
- data/lib/generators/templet/templates/core/app/base_viewer.rb +7 -0
- data/lib/generators/templet/templates/core/app/link_sets/navbar.rb +12 -0
- data/lib/generators/templet/templates/core/app/panel/flash_messages.rb +33 -0
- data/lib/generators/templet/templates/core/app/panel/layout_base.rb +40 -0
- data/lib/generators/templet/templates/core/app/panel/layout_header.rb +22 -0
- data/lib/generators/templet/templates/core/app/panel/layout_header_sidebar.rb +18 -0
- data/lib/generators/templet/templates/core/app/panel/nav.rb +55 -0
- data/lib/generators/templet/templates/core/app/panel/nav_args_option.rb +30 -0
- data/lib/generators/templet/templates/core/app/panel/options_config.rb +19 -0
- data/lib/generators/templet/templates/core/app/panel/show_parents_option.rb +22 -0
- data/lib/generators/templet/templates/core/app/panel/sidebar_links_option.rb +24 -0
- data/lib/generators/templet/templates/core/controllers/.keep +0 -0
- data/lib/generators/templet/templates/core/controllers/json_rendering_helpers.rb +12 -0
- data/lib/generators/templet/templates/core/controllers/rendering_helpers.rb +53 -0
- data/lib/generators/templet/templates/core/controllers/viewer_call_string.rb +144 -0
- data/lib/generators/templet/templates/core/controllers/viewer_call_string_class.rb +65 -0
- data/lib/generators/templet/templates/core/controllers/viewer_responders.rb +121 -0
- data/lib/generators/templet/templates/core/helpers/app.rb +4 -0
- data/lib/generators/templet/templates/core/helpers/templet_helper.rb +10 -0
- data/lib/generators/templet/templates/core/spec/support/apis/api_helper.rb +13 -0
- data/lib/generators/templet/templates/core/spec/support/apis/shared_examples_a_json_controller.rb +196 -0
- data/lib/generators/templet/templates/core/spec/support/core/model_parent_helpers.rb +36 -0
- data/lib/generators/templet/templates/core/spec/support/core/rest_link_procs_assignments.rb +43 -0
- data/lib/generators/templet/templates/core/spec/support/core/rest_link_procs_helpers.rb +15 -0
- data/lib/generators/templet/templates/core/spec/support/viewer/partial_test_helpers.rb +32 -0
- data/lib/generators/templet/templates/core/spec/support/viewer/shared_examples_a_viewer.rb +105 -0
- data/lib/generators/templet/templates/core/spec/templet/forms/bs_form_errors_spec.rb +18 -0
- data/lib/generators/templet/templates/core/spec/templet/forms/bs_form_spec.rb +58 -0
- data/lib/generators/templet/templates/core/spec/templet/layout/html_rails_spec.rb +64 -0
- data/lib/generators/templet/templates/core/spec/templet/links/bs_link_set_collection_spec.rb +79 -0
- data/lib/generators/templet/templates/core/spec/templet/links/rest_link_procs_params_spec.rb +68 -0
- data/lib/generators/templet/templates/core/spec/templet/links/rest_link_procs_parents_spec.rb +78 -0
- data/lib/generators/templet/templates/core/spec/templet/links/rest_link_procs_spec.rb +140 -0
- data/lib/generators/templet/templates/core/templet/constants.rb +54 -0
- data/lib/generators/templet/templates/core/templet/forms/bs_form.rb +73 -0
- data/lib/generators/templet/templates/core/templet/forms/bs_form_errors.rb +38 -0
- data/lib/generators/templet/templates/core/templet/forms/bs_form_field.rb +114 -0
- data/lib/generators/templet/templates/core/templet/forms/bs_form_group.rb +115 -0
- data/lib/generators/templet/templates/core/templet/layouts/html_rails.rb +36 -0
- data/lib/generators/templet/templates/core/templet/links.rb +28 -0
- data/lib/generators/templet/templates/core/templet/links/bs_btn_class.rb +61 -0
- data/lib/generators/templet/templates/core/templet/links/bs_link_set_base.rb +168 -0
- data/lib/generators/templet/templates/core/templet/links/bs_link_set_collection.rb +21 -0
- data/lib/generators/templet/templates/core/templet/links/bs_link_set_navigation.rb +62 -0
- data/lib/generators/templet/templates/core/templet/links/rest_link_procs.rb +163 -0
- data/lib/generators/templet/templates/core/templet/links/rest_link_procs_parent.rb +39 -0
- data/lib/generators/templet/templates/core/templet/links/rest_link_procs_sets.rb +30 -0
- data/lib/generators/templet/templates/core/templet/links/rest_link_text.rb +102 -0
- data/lib/generators/templet/templates/core/templet/links/rest_path.rb +90 -0
- data/lib/generators/templet/templates/core/templet/mixins.rb +9 -0
- data/lib/generators/templet/templates/core/templet/mixins/bs.rb +11 -0
- data/lib/generators/templet/templates/core/templet/mixins/bs/grid.rb +53 -0
- data/lib/generators/templet/templates/core/templet/mixins/bs/lists.rb +77 -0
- data/lib/generators/templet/templates/core/templet/mixins/field_procs.rb +140 -0
- data/lib/generators/templet/templates/core/templet/mixins/html_presenters.rb +82 -0
- data/lib/generators/templet/templates/core/templet/require_all.rb +45 -0
- data/lib/generators/templet/templates/core/templet/utils/html_search_form.rb +53 -0
- data/lib/generators/templet/templates/core/templet/utils/link_set_factory.rb +46 -0
- data/lib/generators/templet/templates/core/templet/utils/link_set_factory_wrapper.rb +53 -0
- data/lib/generators/templet/templates/core/templet/utils/list_model_parents.rb +27 -0
- data/lib/generators/templet/templates/core/templet/utils/navbar_form.rb +26 -0
- data/lib/generators/templet/templates/core/templet/utils/selected_wrapper.rb +40 -0
- data/lib/generators/templet/templates/core/templet/viewer.rb +11 -0
- data/lib/generators/templet/templates/core/templet/viewer/meta_model.rb +66 -0
- data/lib/generators/templet/templates/core/templet/viewer/meta_model_defaults.rb +56 -0
- data/lib/generators/templet/templates/core/templet/viewer/presenters.rb +44 -0
- data/lib/generators/templet/templates/core/templet/viewer/rest_actions.rb +24 -0
- data/lib/generators/templet/templates/core/templet/viewer_base.rb +111 -0
- data/lib/generators/templet/templates/core/templet/viewer_rest.rb +144 -0
- data/lib/generators/templet/viewer/USAGE +10 -0
- data/lib/generators/templet/viewer/viewer_generator.rb +86 -0
- data/lib/templet_rails.rb +5 -0
- data/lib/templet_rails/version.rb +3 -0
- data/templet_rails.gemspec +36 -0
- metadata +178 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Links
|
4
|
+
# For REST links to a model's parent
|
5
|
+
# Included in RestLinkProcs
|
6
|
+
module RestLinkProcsParent
|
7
|
+
def index_parent_link(**options)
|
8
|
+
link_to_parent :index, **options
|
9
|
+
end
|
10
|
+
|
11
|
+
def show_parent_link(**options)
|
12
|
+
link_to_parent :show, **options
|
13
|
+
end
|
14
|
+
|
15
|
+
def new_parent_link(**options)
|
16
|
+
link_to_parent :new, **options
|
17
|
+
end
|
18
|
+
|
19
|
+
def edit_parent_link(**options)
|
20
|
+
link_to_parent :edit, **options
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def link_to_parent(action, grand_parent: nil, text: nil, params: nil)
|
26
|
+
-> renderer, _, parent {
|
27
|
+
|
28
|
+
text ||= link_text.parent_text(action, parent)
|
29
|
+
|
30
|
+
grand_parent = grand_parent && parent.send(grand_parent)
|
31
|
+
|
32
|
+
send("#{action}_link", text, params: params)
|
33
|
+
.(renderer, parent, grand_parent)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Links
|
4
|
+
# For grouping REST links together
|
5
|
+
# Included in RestLinkProcs
|
6
|
+
module RestLinkProcsSets
|
7
|
+
def member_link_hash(field_names=%i(_show _edit _delete),
|
8
|
+
action_to_text: {})
|
9
|
+
field_names.zip(member_links action_to_text: action_to_text).to_h
|
10
|
+
end
|
11
|
+
|
12
|
+
def collection_link_list(renderer, model_name: nil,
|
13
|
+
model: nil, parent: nil)
|
14
|
+
collection_links(model_name).map {|f| f.(renderer, model, parent) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def member_links(params: nil, action_to_text: {})
|
18
|
+
return show_link(action_to_text[:show], params: params),
|
19
|
+
edit_link(action_to_text[:edit], params: params),
|
20
|
+
delete_link(action_to_text[:delete], params: params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def collection_links(name=nil, params: nil)
|
24
|
+
return index_link(name, params: params),
|
25
|
+
new_link(name, params: params)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,102 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Links
|
4
|
+
# For the text and title on buttons and links
|
5
|
+
class RestLinkText
|
6
|
+
def index(model)
|
7
|
+
model_plural(model)
|
8
|
+
end
|
9
|
+
|
10
|
+
def show(model)
|
11
|
+
'Show'
|
12
|
+
end
|
13
|
+
|
14
|
+
def new(model)
|
15
|
+
'New ' + model_singular(model)
|
16
|
+
end
|
17
|
+
|
18
|
+
def edit(model)
|
19
|
+
'Edit'
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete(model)
|
23
|
+
'Delete'
|
24
|
+
end
|
25
|
+
|
26
|
+
def by_path(path)
|
27
|
+
String === path ? path.tr('/', ' ').strip.capitalize : 'Go'
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_title(action, model)
|
31
|
+
case action
|
32
|
+
when :index
|
33
|
+
title_plural(action, model)
|
34
|
+
when :new
|
35
|
+
title_singular(action, model)
|
36
|
+
when :show, :edit, :delete
|
37
|
+
title_info(action, model)
|
38
|
+
else
|
39
|
+
title_plural(action, model)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def parent_text(action, parent)
|
44
|
+
if action == :index
|
45
|
+
prefix, suffix = parent.model_name.plural
|
46
|
+
else
|
47
|
+
prefix, suffix = action, parent.model_name.singular
|
48
|
+
end
|
49
|
+
|
50
|
+
"#{prefix.to_s.capitalize} #{suffix}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def title_singular(action, model)
|
54
|
+
title action, model_singular(model)
|
55
|
+
end
|
56
|
+
|
57
|
+
def title_plural(action, model)
|
58
|
+
title action, model_plural(model)
|
59
|
+
end
|
60
|
+
|
61
|
+
def title_info(action, model)
|
62
|
+
title action, model_info(model)
|
63
|
+
end
|
64
|
+
|
65
|
+
def title(action, model_text)
|
66
|
+
"#{action_alias action} #{model_text}"
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def model_plural(model)
|
72
|
+
if model.respond_to?(:model_name)
|
73
|
+
model.model_name.plural
|
74
|
+
else
|
75
|
+
model.to_s.pluralize
|
76
|
+
end.capitalize.tr('_', ' ')
|
77
|
+
end
|
78
|
+
|
79
|
+
def model_singular(model)
|
80
|
+
if model.respond_to?(:model_name)
|
81
|
+
model.model_name.singular
|
82
|
+
else
|
83
|
+
model.to_s
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def model_info(model)
|
88
|
+
"#{model.model_name.singular}: #{model}"
|
89
|
+
end
|
90
|
+
|
91
|
+
def action_alias(action)
|
92
|
+
{ index: 'List',
|
93
|
+
show: 'See',
|
94
|
+
new: 'Create',
|
95
|
+
edit: 'Update',
|
96
|
+
delete: 'Remove',
|
97
|
+
}[action] || action.to_s.capitalize
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Links
|
4
|
+
# Returns REST paths as an Array
|
5
|
+
class RestPath
|
6
|
+
attr_accessor :model, :parent, :scope, :controller
|
7
|
+
|
8
|
+
def initialize(model, parent=nil, scope: nil, controller: nil)
|
9
|
+
self.model = model
|
10
|
+
|
11
|
+
self.parent = parent
|
12
|
+
|
13
|
+
self.scope = scope
|
14
|
+
|
15
|
+
self.controller = controller
|
16
|
+
end
|
17
|
+
|
18
|
+
def action(name, params: nil, on_collection: false)
|
19
|
+
[name] + (on_collection || on_collection? ? index(params) : show(params))
|
20
|
+
end
|
21
|
+
|
22
|
+
def for_form(params=nil)
|
23
|
+
if model.persisted?
|
24
|
+
return show(params), model
|
25
|
+
else
|
26
|
+
return index(params), model
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def index(params=nil)
|
31
|
+
[ scope, parent, try_controller(plural), params ].compact
|
32
|
+
end
|
33
|
+
|
34
|
+
def show(params=nil)
|
35
|
+
params = add_model_param(params) if controller
|
36
|
+
|
37
|
+
[ scope, parent, try_controller(model, :singular), params ].compact
|
38
|
+
end
|
39
|
+
|
40
|
+
#def create(params=nil)
|
41
|
+
# [ scope, parent, try_controller(singular, :singular), params ].compact
|
42
|
+
#end
|
43
|
+
|
44
|
+
def new(params=nil)
|
45
|
+
#%i(new) + create(params)
|
46
|
+
[ :new, scope, parent, try_controller(singular, :singular), params ].compact
|
47
|
+
end
|
48
|
+
|
49
|
+
def edit(params=nil)
|
50
|
+
%i(edit) + show(params)
|
51
|
+
end
|
52
|
+
|
53
|
+
#alias update show
|
54
|
+
|
55
|
+
alias delete show
|
56
|
+
alias destroy show
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def on_collection?
|
61
|
+
String === model || Symbol === model
|
62
|
+
end
|
63
|
+
|
64
|
+
def try_controller(default, inflection=:plural)
|
65
|
+
inflect(controller, inflection) or default
|
66
|
+
end
|
67
|
+
def inflect(word, inflection)
|
68
|
+
word and (inflection == :plural ? word : word.to_s.singularize)
|
69
|
+
end
|
70
|
+
|
71
|
+
def add_model_param(params=nil)
|
72
|
+
(params || {}).merge id: model.to_param
|
73
|
+
end
|
74
|
+
|
75
|
+
def plural
|
76
|
+
singular.to_s.pluralize.to_sym
|
77
|
+
end
|
78
|
+
|
79
|
+
def singular
|
80
|
+
case model
|
81
|
+
when String, Symbol
|
82
|
+
model.to_sym
|
83
|
+
else
|
84
|
+
model.class.name.underscore.to_sym
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Mixins
|
4
|
+
module Bs
|
5
|
+
# For the Bootstrap grid system
|
6
|
+
module Grid
|
7
|
+
include Constants
|
8
|
+
|
9
|
+
# For arranging a row into columns. Divided into twelfths.
|
10
|
+
# +columns+ is a Hash. For example: { 2 => menu, 10 => table }
|
11
|
+
def in_cols(renderer, columns)
|
12
|
+
inner_self = self
|
13
|
+
|
14
|
+
renderer.call do
|
15
|
+
div :row do
|
16
|
+
columns.to_h.reduce [] do |acc, (count, content)|
|
17
|
+
acc << div(inner_self.col_class count) { content }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# For arranging content into stacked rows.
|
24
|
+
# +rows+ is an Array of separate elements, e.g. [ menu, table ]
|
25
|
+
def in_rows(renderer, *rows, cols: nil, offset: nil)
|
26
|
+
rows += yield(renderer) if block_given?
|
27
|
+
|
28
|
+
inner_self = self
|
29
|
+
|
30
|
+
renderer.call do
|
31
|
+
rows.flatten.compact.map do |row|
|
32
|
+
div :row do
|
33
|
+
html_class = inner_self.col_offset_class(offset) +
|
34
|
+
inner_self.col_class(cols)
|
35
|
+
|
36
|
+
div(html_class) { row }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def col_class(cols=nil)
|
43
|
+
"#{BS_COL}#{cols || 12} "
|
44
|
+
end
|
45
|
+
|
46
|
+
def col_offset_class(offset)
|
47
|
+
offset ? "#{BS_COL_OFFSET}#{offset} " : ''
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,77 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Mixins
|
4
|
+
module Bs
|
5
|
+
# Renders various listing and Bootstrap grouping components
|
6
|
+
module Lists
|
7
|
+
include Constants
|
8
|
+
|
9
|
+
include HtmlPresenters
|
10
|
+
|
11
|
+
def in_html_list(renderer, items=nil, **opts)
|
12
|
+
items = yield(renderer) if block_given?
|
13
|
+
|
14
|
+
html_list(renderer, items, **opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
def in_list_group_links(renderer, links)
|
18
|
+
in_html_list renderer, links, html_class: BS_LIST_GROUP,
|
19
|
+
item_class: BS_LIST_GROUP_ITEM
|
20
|
+
end
|
21
|
+
|
22
|
+
def in_list_group_buttons(renderer, links)
|
23
|
+
renderer.call do
|
24
|
+
div(links, BS_LIST_GROUP)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def in_nav(renderer, links, tabs: false, stacked: false)
|
29
|
+
html_class = tabs ? BS_NAV_TABS : BS_NAV_PILLS
|
30
|
+
|
31
|
+
html_class += " #{BS_NAV_PILLS_STACKED}" if stacked
|
32
|
+
|
33
|
+
in_html_list(renderer, links, html_class: html_class)
|
34
|
+
end
|
35
|
+
|
36
|
+
def in_button_group(renderer, links, stacked: false, justified: false)
|
37
|
+
renderer.call do
|
38
|
+
html_class = stacked ? BS_BUTTON_GROUP_VERTICAL : BS_BUTTON_GROUP
|
39
|
+
|
40
|
+
html_class += ' ' + BS_BUTTON_GROUP_JUSTIFIED if justified
|
41
|
+
|
42
|
+
div(links, html_class, role: 'group')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def in_button_toolbar(renderer, toolbar_links, **options)
|
47
|
+
this = self
|
48
|
+
|
49
|
+
renderer.call do
|
50
|
+
div BS_BUTTON_TOOLBAR, role: BS_TOOLBAR do
|
51
|
+
toolbar_links.map do |links|
|
52
|
+
this.in_button_group(renderer, links, **options)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def button_group_dropdown(renderer, links, text, btn_class='btn-default')
|
59
|
+
list = in_html_list(renderer, links, html_class: BS_BUTTON_DROPDOWN)
|
60
|
+
|
61
|
+
atts = { type: "button",
|
62
|
+
class: "btn #{btn_class} dropdown-toggle",
|
63
|
+
data_toggle: "dropdown",
|
64
|
+
aria_haspopup: true,
|
65
|
+
aria_expanded: false }
|
66
|
+
|
67
|
+
caret = " <span class='caret'></span>"
|
68
|
+
|
69
|
+
renderer.call do
|
70
|
+
div(:btn_group) { [ button(text + caret, atts), *list ] }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,140 @@
|
|
1
|
+
|
2
|
+
module Templet
|
3
|
+
module Mixins
|
4
|
+
# Various procs/lambdas for displaying a model's field values
|
5
|
+
# For general purpose components that deal with models of any type
|
6
|
+
module FieldProcs
|
7
|
+
# For retrieving a model's field value (in table rows)
|
8
|
+
# 1st arg will be a renderer instance
|
9
|
+
def proc_call_method(name)
|
10
|
+
Proc.new {|_, model| model.send name }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns a formatted field value
|
14
|
+
def proc_call_method_format(name)
|
15
|
+
value_proc = proc_call_method(name)
|
16
|
+
|
17
|
+
Proc.new {|*args| format_field_by_type value_proc.(*args) }
|
18
|
+
end
|
19
|
+
|
20
|
+
# To pass parameters to a REST index link
|
21
|
+
def proc_index_link_with_params(link_procs, link_text, params)
|
22
|
+
link_procs.index_link(link_text, params: params)
|
23
|
+
end
|
24
|
+
|
25
|
+
# To pass parameters to an arbitrarily named link
|
26
|
+
def proc_action_link_with_params(link_procs, action, link_text, params)
|
27
|
+
link_procs.link_by_action(action, params: params,
|
28
|
+
text: link_text, #title: nil,
|
29
|
+
on_collection: true)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Used in html definition list controls
|
33
|
+
# Returns a Hash of field names by Procs
|
34
|
+
# The Proc returns a field value of a model passed as 1st arg
|
35
|
+
def hash_field_name_by_value_proc(field_names)
|
36
|
+
hash_yield_value(field_names, &:to_proc)
|
37
|
+
end
|
38
|
+
alias show_controls_hash hash_field_name_by_value_proc
|
39
|
+
|
40
|
+
# Used in html table controls
|
41
|
+
|
42
|
+
# Returns a Hash of names by Procs
|
43
|
+
# The Proc returns a field value of a model passed as 2nd arg
|
44
|
+
def hash_field_name_by_proc_call_method(field_names)
|
45
|
+
hash_yield_value(field_names) {|name| proc_call_method name }
|
46
|
+
end
|
47
|
+
alias index_controls_hash hash_field_name_by_proc_call_method
|
48
|
+
|
49
|
+
# To pass the same parameter to a list of index links
|
50
|
+
# Used in html table headings for sorting by field name
|
51
|
+
# Returns a Hash with a html link as the key and a Proc
|
52
|
+
# The Proc returns a field value of a model passed as 2nd arg
|
53
|
+
def hash_proc_link_with_params_by_proc_call_method(field_names,
|
54
|
+
param_name,
|
55
|
+
link_class='',
|
56
|
+
action: nil,
|
57
|
+
formatted: true)
|
58
|
+
link_procs = rest_link_procs(link_class)
|
59
|
+
|
60
|
+
field_names.inject Hash.new do |hash, field_name|
|
61
|
+
params = { param_name => field_name }
|
62
|
+
|
63
|
+
link_text = field_name.to_s.titleize
|
64
|
+
|
65
|
+
link_proc = if action
|
66
|
+
proc_action_link_with_params(link_procs, action,
|
67
|
+
link_text, params)
|
68
|
+
else
|
69
|
+
proc_index_link_with_params(link_procs, link_text, params)
|
70
|
+
end
|
71
|
+
|
72
|
+
value_proc = if formatted
|
73
|
+
proc_call_method_format(field_name)
|
74
|
+
else
|
75
|
+
proc_call_method(field_name)
|
76
|
+
end
|
77
|
+
|
78
|
+
hash.merge link_proc => value_proc
|
79
|
+
end
|
80
|
+
end
|
81
|
+
alias index_controls_sortable_hash hash_proc_link_with_params_by_proc_call_method
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
# Returns a Hash for use in html tables and lists
|
86
|
+
# The key is used in the heading
|
87
|
+
# The value is a Proc that is called for each row
|
88
|
+
def hash_yield_value(keys)
|
89
|
+
keys.inject Hash.new do |acc, key|
|
90
|
+
acc.merge key => yield(key.to_sym)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def format_field_by_type(value)
|
95
|
+
case value
|
96
|
+
when true, false
|
97
|
+
#value ? 'Yes' : 'No'
|
98
|
+
value ? icon('ok') : icon('remove')
|
99
|
+
when Integer
|
100
|
+
pull_right value.to_s + ' ' * 3
|
101
|
+
when Date
|
102
|
+
value.to_s :rfc822
|
103
|
+
when Time, DateTime
|
104
|
+
value.strftime '%c'
|
105
|
+
else
|
106
|
+
value
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def pull_right(content=nil)
|
111
|
+
"<span class='pull-right'>#{content}</span>".html_safe
|
112
|
+
end
|
113
|
+
def pull_right_proc(function)
|
114
|
+
Proc.new {|*args| pull_right function.(*args) }
|
115
|
+
end
|
116
|
+
|
117
|
+
# For displaying REST links as icons (on tables, for example)
|
118
|
+
|
119
|
+
def icon(name)
|
120
|
+
"<span class='glyphicon glyphicon-#{name}'></span>".html_safe
|
121
|
+
end
|
122
|
+
def actions_to_icons
|
123
|
+
{ show: icon('camera'), edit: icon('pencil'), delete: icon('trash') }
|
124
|
+
end
|
125
|
+
def member_links_hash_as_icons(size=:md)
|
126
|
+
rest_link_procs([:link, size])
|
127
|
+
.member_link_hash(action_to_text: actions_to_icons)
|
128
|
+
end
|
129
|
+
|
130
|
+
# This is a factory that produces Procs for rendering html links
|
131
|
+
# The Proc is passed the current model and parent
|
132
|
+
def rest_link_procs(html_class=nil, remote=remote?)
|
133
|
+
Templet::Links.rest_link_procs html_class, scope: controllers_path,
|
134
|
+
controller: controllers_name,
|
135
|
+
remote: remote
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|