dynamic_scaffold 1.4.3 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c78d85fe028cf2f3a8d6dbb2535c1763cb44af9dbd23e8f2c209900cb697c5b
4
- data.tar.gz: ba48a0bb59375db1313e69510a300df75b2e219df6da94a19033e122742b9644
3
+ metadata.gz: 83178747c82566649957d2845a6e425dd16b320b8fe8ef3954432981c941aa16
4
+ data.tar.gz: c8e689ee3fd60cb006f29cf053009de07799b45542c6187f91332eca005d2046
5
5
  SHA512:
6
- metadata.gz: 0bb514fae7e68f907888a1e0d3642cb1b2991f66c5ed4be7397180e6cc58b8e82bf4e37cc16400eb6f0201ad216fb9a7e5e6dc7a9d5bd94959b82dc6b96c7551
7
- data.tar.gz: 6b2d83755e283de98609d4ec9bf25a8dde3a8fc5a2160a9c6770b2673eec0c15d01022138e8f38daf5601a4cebff8cce5dce5bcc9e1be89987f402daa7d9bc0c
6
+ metadata.gz: b4fb71172557f91a3241555f6469ec2bb9c8aa7f264b0c284a134c05f4392f8b362c46dcf53a9447ba4efb91188939f066337286bca694f55fb6b71025cc16c7
7
+ data.tar.gz: 3fc6eadf855760330b0840a4148f18a37a42a4aa2d37e433b5c9c09a9293a4d1571f8adb80e0ee70932be77903dab830dcc324080b1d7196604961d5009f77f3
data/README.md CHANGED
@@ -170,6 +170,11 @@ class ShopController < ApplicationController
170
170
  # config.list.title do |record|
171
171
  # record.name
172
172
  # end
173
+
174
+ # You can add class to list page's each row.
175
+ config.list.row_class do |record|
176
+ 'disabled' unless record.active?
177
+ end
173
178
 
174
179
  # First arg is attribute name of model.
175
180
  # Last hash arg is given to HTML attributes options.
@@ -359,6 +364,12 @@ We support [cocoon](https://github.com/nathanvda/cocoon).
359
364
  end
360
365
  ```
361
366
 
367
+ ##### json_object
368
+
369
+ You can save json object string in a column using each form items and validations.
370
+ Check this [wiki](https://github.com/gomo/dynamic_scaffold/wiki/Handling-json-object-string-column)
371
+
372
+
362
373
  #### Overwrite actions
363
374
 
364
375
  You can pass the block to super in index/create/update actions.
@@ -1,6 +1,6 @@
1
1
  <%unless flash[:dynamic_scaffold_danger].nil? -%>
2
2
  <div class="alert alert-danger" role="alert">
3
- <%= flash[:dynamic_scaffold_danger] %>
3
+ <%== flash[:dynamic_scaffold_danger] %>
4
4
  </div>
5
5
  <%end%>
6
6
  <input type="hidden" class="authenticity_param_name" value="<%= request_forgery_protection_token %>">
@@ -30,8 +30,8 @@
30
30
  data-confirm-cancel="<%= t('dynamic_scaffold.message.confirm_cancel') %>"
31
31
  data-confirm-cancel-class="btn btn-outline-secondary btn-default btn-sm"
32
32
  >
33
- <%@records.each do |record|%>
34
- <li class="ds-list-row js-ds-list-row">
33
+ <%@records.each_with_index do |record, index|%>
34
+ <li class="<%= class_names('ds-list-row js-ds-list-row', dynamic_scaffold.list.row_class(record)) %>">
35
35
  <% if dynamic_scaffold.list.title? %>
36
36
  <div class="ds-list-heading"><%= dynamic_scaffold.list.title(record) %></div>
37
37
  <% end %>
@@ -3,9 +3,9 @@
3
3
  <%= elem.render(self, form) %>
4
4
  <% else %>
5
5
  <%- errors = elem.errors(form) -%>
6
- <div class="<%= class_names('form-group', 'has-error': errors.present?) %>">
6
+ <div class="<%= class_names('form-group', 'has-error': errors.present?) %>" data-name="<%= elem.unique_name %>">
7
7
  <%= elem.render_label(self, depth) %>
8
- <div>
8
+ <div class="ds-form-item">
9
9
  <%- elem.insert(:before).each do |block| -%>
10
10
  <%= self.instance_exec(@record, &block) %>
11
11
  <%-end-%>
@@ -68,7 +68,7 @@
68
68
  <%end%>
69
69
  <% end %>
70
70
  <% elsif elem.type? :cocoon %>
71
- <%= form.fields_for elem.name, elem.build_children(@record) do |child_form| %>
71
+ <%= form.fields_for(elem.name, elem.build_children(@record)) do |child_form| %>
72
72
  <%= render 'dynamic_scaffold/bootstrap/form/cocoon', f: child_form, items: elem.form.items, depth: depth %>
73
73
  <% end %>
74
74
  <%= link_to_add_association(
@@ -86,6 +86,12 @@
86
86
  .gsub(/ : /, '<span class="mr-2" style="display: inline-block;">:</span>')
87
87
  %>
88
88
  </div>
89
+ <% elsif elem.type?(:json_object) %>
90
+ <%= form.fields_for(elem.name, @record.public_send(elem.name)) do |child_form| %>
91
+ <%- elem.form.items.each do |child_elem|-%>
92
+ <%= render 'dynamic_scaffold/bootstrap/form/row', form: child_form, elem: child_elem, depth: depth %>
93
+ <% end %>
94
+ <% end %>
89
95
  <% else %>
90
96
  <%= elem.render(self, form, class_names('form-control', {'is-invalid': errors.present?})) %>
91
97
  <% end %>
@@ -32,8 +32,14 @@ module DynamicScaffold
32
32
  autoload :SingleOption, 'dynamic_scaffold/form/item/single_option'
33
33
  autoload :TwoOptionsWithBlock, 'dynamic_scaffold/form/item/two_options_with_block'
34
34
  autoload :TwoOptions, 'dynamic_scaffold/form/item/two_options'
35
- autoload :GlobalizeFields, 'dynamic_scaffold/form/item/globalize_fields'
36
- autoload :Cocoon, 'dynamic_scaffold/form/item/cocoon'
35
+ autoload :GlobalizeFields, 'dynamic_scaffold/form/item/globalize_fields'
36
+ autoload :Cocoon, 'dynamic_scaffold/form/item/cocoon'
37
+ autoload :JSONObject, 'dynamic_scaffold/form/item/json_object'
37
38
  end
38
39
  end
40
+
41
+ module JSONObject
42
+ autoload :Attribute, 'dynamic_scaffold/json_object/attribute'
43
+ autoload :Model, 'dynamic_scaffold/json_object/model'
44
+ end
39
45
  end
@@ -77,7 +77,7 @@ module DynamicScaffold
77
77
  end
78
78
  end
79
79
 
80
- def update # rubocop:disable Metrics/AbcSize
80
+ def update # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
81
81
  values = update_values
82
82
  @record = find_record(dynamic_scaffold.model.primary_key => params['id'])
83
83
  datetime_select_keys = []
@@ -93,6 +93,9 @@ module DynamicScaffold
93
93
  # globalize
94
94
  next [:translations_attributes, @record.translations] if k == 'translations_attributes'
95
95
 
96
+ # skip nested_attributes
97
+ next unless @record.respond_to? k
98
+
96
99
  [k, @record.public_send(k)]
97
100
  end.compact.to_h.with_indifferent_access
98
101
 
@@ -107,7 +110,7 @@ module DynamicScaffold
107
110
  end
108
111
  end
109
112
 
110
- def destroy
113
+ def destroy # rubocop:disable Metrics/AbcSize
111
114
  # `Destroy` also does not support multiple primary keys too.
112
115
  record = find_record(dynamic_scaffold.model.primary_key => params['id'])
113
116
  begin
@@ -122,6 +125,9 @@ module DynamicScaffold
122
125
  flash[:dynamic_scaffold_danger] = I18n.t('dynamic_scaffold.alert.destroy.failed')
123
126
  logger.error(e)
124
127
  end
128
+
129
+ flash[:dynamic_scaffold_danger] = record.errors[:base].join('<br>') if record.errors[:base].any?
130
+
125
131
  redirect_to dynamic_scaffold_path(:index, request_queries)
126
132
  end
127
133
 
@@ -15,6 +15,7 @@ module DynamicScaffold
15
15
  end
16
16
  end
17
17
 
18
+ attr_accessor :parent_item
18
19
  attr_reader :name
19
20
  def initialize(config, type, name, html_attributes = {})
20
21
  @config = config
@@ -31,6 +32,13 @@ module DynamicScaffold
31
32
  @label_block = nil
32
33
  end
33
34
 
35
+ def unique_name
36
+ results = [@config.model.table_name]
37
+ results << parent_item.name if parent_item.present?
38
+ results << name
39
+ results.join('_')
40
+ end
41
+
34
42
  def notes?
35
43
  !@notes.empty?
36
44
  end
@@ -95,7 +103,7 @@ module DynamicScaffold
95
103
  # return label unless label.nil?
96
104
  # end
97
105
 
98
- view.tag.label(proxy_field.label, label_attrs)
106
+ view.tag.label(proxy_field.label, label_attrs) if proxy_field.label.present?
99
107
  end
100
108
 
101
109
  def extract_parameters(permitting)
@@ -7,6 +7,7 @@ module DynamicScaffold
7
7
  super
8
8
  @options = options
9
9
  @form = FormBuilder.new(config)
10
+ @form.parent_item = self
10
11
  yield(@form)
11
12
  end
12
13
 
@@ -0,0 +1,32 @@
1
+ module DynamicScaffold
2
+ module Form
3
+ module Item
4
+ class JSONObject < Base
5
+ attr_reader :form
6
+ def initialize(config, type, name, options = {})
7
+ super
8
+ @options = options
9
+ @form = FormBuilder.new(config)
10
+ @form.parent_item = self
11
+ yield(@form)
12
+ end
13
+
14
+ # the lable is always empty.
15
+ def render_label(_view, _depth)
16
+ ''
17
+ end
18
+
19
+ def extract_parameters(permitting)
20
+ hash = permitting.find {|e| e.is_a?(Hash) && e.key?(name) }
21
+ if hash.nil?
22
+ hash = {}
23
+ hash[name] = form.items.map(&:name)
24
+ permitting << hash
25
+ else
26
+ hash[name].concat(form.items.map(&:name))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -30,7 +30,9 @@ module DynamicScaffold
30
30
 
31
31
  globalize_fields: Form::Item::GlobalizeFields,
32
32
 
33
- cocoon: Form::Item::Cocoon
33
+ cocoon: Form::Item::Cocoon,
34
+
35
+ json_object: Form::Item::JSONObject
34
36
  }.freeze
35
37
  end
36
38
  end
@@ -1,5 +1,7 @@
1
1
  module DynamicScaffold
2
2
  class FormBuilder
3
+ attr_accessor :parent_item
4
+
3
5
  def initialize(config)
4
6
  @config = config
5
7
  @items = []
@@ -11,7 +13,9 @@ module DynamicScaffold
11
13
  @config.model.column_names.each do |column|
12
14
  type = :text_field
13
15
  type = :hidden_field if @config.scope && @config.scope.include?(column.to_sym)
14
- @items << Form::Item::SingleOption.new(@config, type, column)
16
+ item = Form::Item::SingleOption.new(@config, type, column)
17
+ item.parent_item = parent_item
18
+ @items << item
15
19
  end
16
20
  end
17
21
  @items
@@ -28,6 +32,7 @@ module DynamicScaffold
28
32
 
29
33
  def item(type, *args, &block)
30
34
  item = Form::Item::Base.create(@config, type, *args, &block)
35
+ item.parent_item = parent_item
31
36
  @items << item
32
37
  item
33
38
  end
@@ -0,0 +1,24 @@
1
+ module DynamicScaffold
2
+ module JSONObject
3
+ module Attribute
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ @json_object_attribute_names ||= []
8
+
9
+ define_method :valid? do |context = nil|
10
+ result = super(context)
11
+ json_object_attribute_names = self.class.instance_variable_get(:@json_object_attribute_names)
12
+ json_object_attribute_names.all? {|method| public_send(method).valid?(context) } && result
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def json_object_attributte(attribute_name, model)
18
+ @json_object_attribute_names << attribute_name
19
+ serialize attribute_name, model
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ module DynamicScaffold
2
+ module JSONObject
3
+ module Model
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def dump(obj)
8
+ obj = obj.attributes if obj.is_a? ActiveModel::Attributes
9
+ obj.to_json if obj
10
+ end
11
+
12
+ def load(source)
13
+ new(source ? JSON.parse(source) : {})
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -7,6 +7,7 @@ module DynamicScaffold
7
7
  @order = []
8
8
  @title = nil
9
9
  @filter = nil
10
+ @row_class_block = nil
10
11
  end
11
12
 
12
13
  def pagination(options = nil)
@@ -88,5 +89,13 @@ module DynamicScaffold
88
89
  @filter = block if block_given?
89
90
  @filter
90
91
  end
92
+
93
+ def row_class(record = nil, &block)
94
+ if block_given?
95
+ @row_class_block = block
96
+ elsif record.present? && @row_class_block
97
+ @config.controller.view_context.instance_exec(record, &@row_class_block)
98
+ end
99
+ end
91
100
  end
92
101
  end
@@ -1,3 +1,3 @@
1
1
  module DynamicScaffold
2
- VERSION = '1.4.3'.freeze
2
+ VERSION = '1.8.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masamoto Miyata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-05 00:00:00.000000000 Z
11
+ date: 2021-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: classnames-rails-view
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: '5.0'
48
48
  - - "<="
49
49
  - !ruby/object:Gem::Version
50
- version: '6.0'
50
+ version: '6.1'
51
51
  type: :runtime
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
@@ -57,7 +57,7 @@ dependencies:
57
57
  version: '5.0'
58
58
  - - "<="
59
59
  - !ruby/object:Gem::Version
60
- version: '6.0'
60
+ version: '6.1'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: capybara
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -295,12 +295,15 @@ files:
295
295
  - lib/dynamic_scaffold/form/item/carrier_wave_image.rb
296
296
  - lib/dynamic_scaffold/form/item/cocoon.rb
297
297
  - lib/dynamic_scaffold/form/item/globalize_fields.rb
298
+ - lib/dynamic_scaffold/form/item/json_object.rb
298
299
  - lib/dynamic_scaffold/form/item/single_option.rb
299
300
  - lib/dynamic_scaffold/form/item/two_options.rb
300
301
  - lib/dynamic_scaffold/form/item/two_options_with_block.rb
301
302
  - lib/dynamic_scaffold/form/item/type.rb
302
303
  - lib/dynamic_scaffold/form_builder.rb
303
304
  - lib/dynamic_scaffold/icons/fontawesome.rb
305
+ - lib/dynamic_scaffold/json_object/attribute.rb
306
+ - lib/dynamic_scaffold/json_object/model.rb
304
307
  - lib/dynamic_scaffold/list/item.rb
305
308
  - lib/dynamic_scaffold/list/pagination.rb
306
309
  - lib/dynamic_scaffold/list_builder.rb