dry_crud 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +8 -8
  2. data/README.rdoc +67 -33
  3. data/VERSION +1 -1
  4. data/{lib/generators/dry_crud/templates/app → app}/assets/images/actions/add.png +0 -0
  5. data/{lib/generators/dry_crud/templates/app → app}/assets/images/actions/delete.png +0 -0
  6. data/{lib/generators/dry_crud/templates/app → app}/assets/images/actions/edit.png +0 -0
  7. data/{lib/generators/dry_crud/templates/app → app}/assets/images/actions/list.png +0 -0
  8. data/{lib/generators/dry_crud/templates/app → app}/assets/images/actions/show.png +0 -0
  9. data/{lib/generators/dry_crud/templates/app → app}/assets/stylesheets/crud.scss +12 -22
  10. data/{lib/generators/dry_crud/templates/app → app}/assets/stylesheets/sample.scss +157 -106
  11. data/{lib/generators/dry_crud/templates/app → app}/controllers/crud_controller.rb +15 -16
  12. data/{lib/generators/dry_crud/templates/app/controllers/crud → app/controllers/dry_crud}/generic_model.rb +7 -7
  13. data/{lib/generators/dry_crud/templates/app/controllers/crud → app/controllers/dry_crud}/nestable.rb +2 -2
  14. data/{lib/generators/dry_crud/templates/app/controllers/crud → app/controllers/dry_crud}/rememberable.rb +3 -3
  15. data/{lib/generators/dry_crud/templates/app/controllers/crud → app/controllers/dry_crud}/render_callbacks.rb +2 -2
  16. data/{lib/generators/dry_crud/templates/app/controllers/crud → app/controllers/dry_crud}/responder.rb +2 -2
  17. data/app/controllers/dry_crud/searchable.rb +77 -0
  18. data/{lib/generators/dry_crud/templates/app/controllers/crud → app/controllers/dry_crud}/sortable.rb +9 -7
  19. data/{lib/generators/dry_crud/templates/app → app}/controllers/list_controller.rb +11 -9
  20. data/{lib/generators/dry_crud/templates/app → app}/helpers/actions_helper.rb +2 -2
  21. data/{lib/generators/dry_crud/templates/app/helpers/crud/form_builder.rb → app/helpers/dry_crud/form/builder.rb} +147 -164
  22. data/app/helpers/dry_crud/form/control.rb +180 -0
  23. data/app/helpers/dry_crud/table/actions.rb +88 -0
  24. data/app/helpers/dry_crud/table/builder.rb +115 -0
  25. data/app/helpers/dry_crud/table/col.rb +25 -0
  26. data/app/helpers/dry_crud/table/sorting.rb +61 -0
  27. data/{lib/generators/dry_crud/templates/app → app}/helpers/form_helper.rb +3 -2
  28. data/{lib/generators/dry_crud/templates/app → app}/helpers/format_helper.rb +5 -5
  29. data/{lib/generators/dry_crud/templates/app → app}/helpers/i18n_helper.rb +2 -2
  30. data/{lib/generators/dry_crud/templates/app → app}/helpers/table_helper.rb +8 -7
  31. data/{lib/generators/dry_crud/templates/app → app}/helpers/utility_helper.rb +1 -1
  32. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_actions_edit.html.erb +0 -0
  33. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_actions_edit.html.haml +0 -0
  34. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_actions_index.html.erb +0 -0
  35. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_actions_index.html.haml +0 -0
  36. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_actions_show.html.erb +0 -0
  37. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_actions_show.html.haml +0 -0
  38. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_attrs.html.erb +0 -0
  39. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_attrs.html.haml +0 -0
  40. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_form.html.erb +0 -0
  41. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_form.html.haml +0 -0
  42. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_list.html.erb +0 -0
  43. data/{lib/generators/dry_crud/templates/app → app}/views/crud/_list.html.haml +0 -0
  44. data/{lib/generators/dry_crud/templates/app → app}/views/crud/edit.html.erb +0 -0
  45. data/{lib/generators/dry_crud/templates/app → app}/views/crud/edit.html.haml +0 -0
  46. data/{lib/generators/dry_crud/templates/app → app}/views/crud/new.html.erb +0 -0
  47. data/{lib/generators/dry_crud/templates/app → app}/views/crud/new.html.haml +0 -0
  48. data/{lib/generators/dry_crud/templates/app → app}/views/crud/show.html.erb +0 -0
  49. data/{lib/generators/dry_crud/templates/app → app}/views/crud/show.html.haml +0 -0
  50. data/{lib/generators/dry_crud/templates/app → app}/views/layouts/_flash.html.erb +0 -0
  51. data/{lib/generators/dry_crud/templates/app → app}/views/layouts/_flash.html.haml +0 -0
  52. data/{lib/generators/dry_crud/templates/app → app}/views/layouts/_nav.html.erb +2 -2
  53. data/{lib/generators/dry_crud/templates/app → app}/views/layouts/_nav.html.haml +2 -2
  54. data/{lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb → app/views/layouts/application.html.erb} +22 -16
  55. data/{lib/generators/dry_crud/templates/app/views/layouts/crud.html.haml → app/views/layouts/application.html.haml} +17 -11
  56. data/{lib/generators/dry_crud/templates/app → app}/views/list/_actions_index.html.erb +0 -0
  57. data/{lib/generators/dry_crud/templates/app → app}/views/list/_actions_index.html.haml +0 -0
  58. data/{lib/generators/dry_crud/templates/app → app}/views/list/_list.html.erb +0 -0
  59. data/{lib/generators/dry_crud/templates/app → app}/views/list/_list.html.haml +0 -0
  60. data/app/views/list/_search.html.erb +10 -0
  61. data/app/views/list/_search.html.haml +7 -0
  62. data/{lib/generators/dry_crud/templates/app → app}/views/list/index.html.erb +0 -0
  63. data/{lib/generators/dry_crud/templates/app → app}/views/list/index.html.haml +0 -0
  64. data/{lib/generators/dry_crud/templates/app → app}/views/shared/_error_messages.html.erb +1 -1
  65. data/{lib/generators/dry_crud/templates/app → app}/views/shared/_error_messages.html.haml +1 -1
  66. data/{lib/generators/dry_crud/templates/app → app}/views/shared/_labeled.html.erb +0 -0
  67. data/{lib/generators/dry_crud/templates/app → app}/views/shared/_labeled.html.haml +0 -0
  68. data/{lib/generators/dry_crud/templates/config → config}/locales/crud.de.yml +0 -0
  69. data/{lib/generators/dry_crud/templates/config → config}/locales/crud.en.yml +0 -0
  70. data/lib/dry_crud/engine.rb +18 -0
  71. data/lib/dry_crud.rb +5 -0
  72. data/lib/generators/dry_crud/dry_crud_generator.rb +12 -20
  73. data/lib/generators/dry_crud/dry_crud_generator_base.rb +57 -0
  74. data/lib/generators/dry_crud/file_generator.rb +41 -0
  75. data/lib/generators/dry_crud/templates/INSTALL +1 -1
  76. data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +1 -1
  77. data/lib/generators/dry_crud/templates/spec/helpers/{crud/form_builder_spec.rb → dry_crud/form/builder_spec.rb} +28 -19
  78. data/lib/generators/dry_crud/templates/spec/helpers/{crud/table_builder_spec.rb → dry_crud/table/builder_spec.rb} +3 -3
  79. data/lib/generators/dry_crud/templates/spec/helpers/form_helper_spec.rb +21 -40
  80. data/lib/generators/dry_crud/templates/spec/helpers/format_helper_spec.rb +36 -26
  81. data/lib/generators/dry_crud/templates/spec/helpers/table_helper_spec.rb +4 -4
  82. data/lib/generators/dry_crud/templates/spec/helpers/utility_helper_spec.rb +6 -6
  83. data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +1 -1
  84. data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +0 -1
  85. data/lib/generators/dry_crud/templates/test/helpers/{crud/form_builder_test.rb → dry_crud/form/builder_test.rb} +38 -25
  86. data/lib/generators/dry_crud/templates/test/helpers/{crud/table_builder_test.rb → dry_crud/table/builder_test.rb} +4 -4
  87. data/lib/generators/dry_crud/templates/test/helpers/form_helper_test.rb +12 -9
  88. data/lib/generators/dry_crud/templates/test/helpers/format_helper_test.rb +13 -1
  89. data/lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb +4 -4
  90. data/lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb +3 -3
  91. data/lib/generators/dry_crud/templates/test/support/crud_test_model.rb +2 -2
  92. metadata +76 -68
  93. data/lib/generators/dry_crud/templates/app/controllers/crud/searchable.rb +0 -55
  94. data/lib/generators/dry_crud/templates/app/helpers/crud/table_builder.rb +0 -280
  95. data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +0 -6
  96. data/lib/generators/dry_crud/templates/app/views/list/_search.html.haml +0 -5
@@ -1,280 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module Crud
4
- # A simple helper to easily define tables listing several rows of the same
5
- # data type.
6
- #
7
- # Example Usage:
8
- # Crud::TableBuilder.table(entries, template) do |t|
9
- # t.col('My Header', class: 'css') {|e| link_to 'Show', e }
10
- # t.attrs :name, :city
11
- # end
12
- class TableBuilder
13
- attr_reader :entries, :cols, :options, :template
14
-
15
- delegate :content_tag, :format_attr, :column_type, :association, :dom_id,
16
- :captionize, :add_css_class, :content_tag_nested,
17
- to: :template
18
-
19
- def initialize(entries, template, options = {})
20
- @entries = entries
21
- @template = template
22
- @options = options
23
- @cols = []
24
- end
25
-
26
- # Convenience method to directly generate a table. Renders a row for each
27
- # entry in entries. Takes a block that gets the table object as parameter
28
- # for configuration. Returns the generated html for the table.
29
- def self.table(entries, template, options = {})
30
- t = new(entries, template, options)
31
- yield t
32
- t.to_html
33
- end
34
-
35
- # Define a column for the table with the given header, the html_options
36
- # used for each td and a block rendering the contents of a cell for the
37
- # current entry. The columns appear in the order they are defined.
38
- def col(header = '', html_options = {}, &block)
39
- @cols << Col.new(header, html_options, @template, block)
40
- end
41
-
42
- # Convenience method to add one or more attribute columns.
43
- # The attribute name will become the header, the cells will contain
44
- # the formatted attribute value for the current entry.
45
- def attrs(*attrs)
46
- attrs.each do |a|
47
- attr(a)
48
- end
49
- end
50
-
51
- # Define a column for the given attribute and an optional header.
52
- # If no header is given, the attribute name is used. The cell will
53
- # contain the formatted attribute value for the current entry.
54
- def attr(a, header = nil, html_options = {}, &block)
55
- header ||= attr_header(a)
56
- block ||= ->(e) { format_attr(e, a) }
57
- add_css_class(html_options, align_class(a))
58
- col(header, html_options, &block)
59
- end
60
-
61
- # Renders the table as HTML.
62
- def to_html
63
- content_tag :table, options do
64
- content_tag(:thead, html_header) +
65
- content_tag_nested(:tbody, entries) { |e| html_row(e) }
66
- end
67
- end
68
-
69
- # Returns css classes used for alignment of the cell data.
70
- # Based on the column type of the attribute.
71
- def align_class(attr)
72
- entry = entries.present? ? entry_class.new : nil
73
- case column_type(entry, attr)
74
- when :integer, :float, :decimal
75
- 'right' unless association(entry, attr, :belongs_to)
76
- when :boolean
77
- 'center'
78
- end
79
- end
80
-
81
- # Creates a header string for the given attr.
82
- def attr_header(attr)
83
- captionize(attr, entry_class)
84
- end
85
-
86
- private
87
-
88
- # Renders the header row of the table.
89
- def html_header
90
- content_tag_nested(:tr, cols) { |c| c.html_header }
91
- end
92
-
93
- # Renders a table row for the given entry.
94
- def html_row(entry)
95
- attrs = {}
96
- attrs[:id] = dom_id(entry) if entry.respond_to?(:to_key)
97
- content_tag_nested(:tr, cols, attrs) { |c| c.html_cell(entry) }
98
- end
99
-
100
- # Determines the class of the table entries.
101
- # All entries should be of the same type.
102
- def entry_class
103
- if entries.respond_to?(:klass)
104
- entries.klass
105
- else
106
- entries.first.class
107
- end
108
- end
109
-
110
- # Helper class to store column information.
111
- class Col < Struct.new(:header, :html_options, :template, :block) #:nodoc:
112
-
113
- delegate :content_tag, :capture, to: :template
114
-
115
- # Runs the Col block for the given entry.
116
- def content(entry)
117
- entry.nil? ? '' : capture(entry, &block)
118
- end
119
-
120
- # Renders the header cell of the Col.
121
- def html_header
122
- content_tag(:th, header, html_options)
123
- end
124
-
125
- # Renders a table cell for the given entry.
126
- def html_cell(entry)
127
- content_tag(:td, content(entry), html_options)
128
- end
129
-
130
- end
131
-
132
- # Provides headers with sort links. Expects a method :sortable?(attr)
133
- # in the template/controller to tell if an attribute is sortable or not.
134
- # Extracted into an own module for convenience.
135
- module Sorting
136
- # Create a header with sort links and a mark for the current sort
137
- # direction.
138
- def sort_header(attr, label = nil)
139
- label ||= attr_header(attr)
140
- template.link_to(label, sort_params(attr)) + current_mark(attr)
141
- end
142
-
143
- # Same as :attrs, except that it renders a sort link in the header
144
- # if an attr is sortable.
145
- def sortable_attrs(*attrs)
146
- attrs.each { |a| sortable_attr(a) }
147
- end
148
-
149
- # Renders a sort link header, otherwise similar to :attr.
150
- def sortable_attr(a, header = nil, &block)
151
- if template.sortable?(a)
152
- attr(a, sort_header(a, header), &block)
153
- else
154
- attr(a, header, &block)
155
- end
156
- end
157
-
158
- private
159
-
160
- # Request params for the sort link.
161
- def sort_params(attr)
162
- params.merge({ sort: attr, sort_dir: sort_dir(attr) })
163
- end
164
-
165
- # The sort mark, if any, for the given attribute.
166
- def current_mark(attr)
167
- if current_sort?(attr)
168
- (sort_dir(attr) == 'asc' ? ' &uarr;' : ' &darr;').html_safe
169
- else
170
- ''
171
- end
172
- end
173
-
174
- # Returns true if the given attribute is the current sort column.
175
- def current_sort?(attr)
176
- params[:sort] == attr.to_s
177
- end
178
-
179
- # The sort direction to use in the sort link for the given attribute.
180
- def sort_dir(attr)
181
- current_sort?(attr) && params[:sort_dir] == 'asc' ? 'desc' : 'asc'
182
- end
183
-
184
- # Delegate to template.
185
- def params
186
- template.params
187
- end
188
- end
189
-
190
- include Sorting
191
-
192
- # Adds action columns to the table builder.
193
- # Predefined actions are available for show, edit and destroy.
194
- # Additionally, a special col type to define cells linked to the show page
195
- # of the row entry is provided.
196
- module Actions
197
- extend ActiveSupport::Concern
198
-
199
- included do
200
- delegate :link_to, :path_args, :edit_polymorphic_path, :ti,
201
- to: :template
202
- end
203
-
204
- # Renders the passed attr with a link to the show action for
205
- # the current entry.
206
- # A block may be given to define the link path for the row entry.
207
- def attr_with_show_link(attr, &block)
208
- sortable_attr(attr) do |e|
209
- link_to(format_attr(e, attr), action_path(e, &block))
210
- end
211
- end
212
-
213
- # Action column to show the row entry.
214
- # A block may be given to define the link path for the row entry.
215
- # If the block returns nil, no link is rendered.
216
- def show_action_col(html_options = {}, &block)
217
- action_col do |e|
218
- path = action_path(e, &block)
219
- if path
220
- table_action_link('zoom-in',
221
- path,
222
- html_options)
223
- end
224
- end
225
- end
226
-
227
- # Action column to edit the row entry.
228
- # A block may be given to define the link path for the row entry.
229
- # If the block returns nil, no link is rendered.
230
- def edit_action_col(html_options = {}, &block)
231
- action_col do |e|
232
- path = action_path(e, &block)
233
- if path
234
- path = path.is_a?(String) ? path : edit_polymorphic_path(path)
235
- table_action_link('pencil', path, html_options)
236
- end
237
- end
238
- end
239
-
240
- # Action column to destroy the row entry.
241
- # A block may be given to define the link path for the row entry.
242
- # If the block returns nil, no link is rendered.
243
- def destroy_action_col(html_options = {}, &block)
244
- action_col do |e|
245
- path = action_path(e, &block)
246
- if path
247
- table_action_link('remove',
248
- path,
249
- html_options.merge(
250
- data: { confirm: ti(:confirm_delete),
251
- method: :delete }))
252
- end
253
- end
254
- end
255
-
256
- # Action column inside a table. No header.
257
- # The cell content should be defined in the passed block.
258
- def action_col(&block)
259
- col('', class: 'action', &block)
260
- end
261
-
262
- # Generic action link inside a table.
263
- def table_action_link(icon, url, html_options = {})
264
- add_css_class(html_options, "icon-#{icon}")
265
- link_to('', url, html_options)
266
- end
267
-
268
- private
269
-
270
- # If a block is given, call it to get the path for the current row entry.
271
- # Otherwise, return the standard path args.
272
- def action_path(e, &block)
273
- block_given? ? yield(e) : path_args(e)
274
- end
275
- end
276
-
277
- include Actions
278
-
279
- end
280
- end
@@ -1,6 +0,0 @@
1
- <%= form_tag(nil, { method: :get, class: 'form-search' }) do %>
2
- <%= hidden_field_tag :returning, true %>
3
- <%= hidden_field_tag :page, 1 %>
4
- <%= search_field_tag :q, params[:q], class: 'search-query' %>
5
- <%= submit_tag ti(:"button.search"), class: 'btn' %>
6
- <% end %>
@@ -1,5 +0,0 @@
1
- = form_tag(nil, { method: :get, class: 'form-search' }) do
2
- = hidden_field_tag :returning, true
3
- = hidden_field_tag :page, 1
4
- = search_field_tag :q, params[:q], class: 'search-query'
5
- = submit_tag ti(:"button.search"), class: 'btn'