dynamic_scaffold 1.5.0 → 1.8.1

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: ad52f099101bd86041d179bffaecf6618dd19066823e9c0309a4aa5323b04638
4
- data.tar.gz: bcf20b02b4afe8ea15c22753d1a02e9878a0d2c5db43bf6223b3e5a20e87da76
3
+ metadata.gz: 3125035e4f7e839f4277b447b187755759e3ee6cfcdc5c4c9a926bd78cbb9da5
4
+ data.tar.gz: 981d59b31ace4c0a528139b09a277347598d255de8ff31ef27e822198adeb77a
5
5
  SHA512:
6
- metadata.gz: 51d794aa0eeb3166b3fdd9a1d583d9e081498ef14e6314dcb2ab04863d4c29811774eccbf7f6f9f06a6953e29ca92088744505afd661513a2e13647bb4f4d6f4
7
- data.tar.gz: 481e2b94fe39f8aab2cb542f51ba9b923d170da1b9d15250675cae364f6c0e55c4e1f713edafc8f2112e525daf0fbb29698fc8924a26bccbe13c520a2fa04793
6
+ metadata.gz: a8104becf673ebdfc863b1bd0d88bd148b68320c1d60068957dc1080640a4543a5c972562dc0713842dee33a21d405a2de61105b84bc78077a28e9db84ac7c33
7
+ data.tar.gz: 82c701212363527a4ec1b74c1401080a86286faf11708e55dd610364e98696d5c17748c44226010ca19d999549c3cfe16829af2145c66f84c5febd284c4e07c1
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 %>
@@ -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,7 +15,8 @@ module DynamicScaffold
15
15
  end
16
16
  end
17
17
 
18
- attr_reader :name
18
+ attr_accessor :parent_item
19
+ attr_reader :name, :multiple
19
20
  def initialize(config, type, name, html_attributes = {})
20
21
  @config = config
21
22
  @name = name
@@ -32,7 +33,10 @@ module DynamicScaffold
32
33
  end
33
34
 
34
35
  def unique_name
35
- "#{@config.model.table_name}_#{name}"
36
+ results = [@config.model.table_name]
37
+ results << parent_item.name if parent_item.present?
38
+ results << name
39
+ results.join('_')
36
40
  end
37
41
 
38
42
  def notes?
@@ -2,8 +2,8 @@ module DynamicScaffold
2
2
  module Form
3
3
  module Item
4
4
  class Block < Base
5
- def initialize(config, type, name, &block)
6
- super(config, type, name, {})
5
+ def initialize(config, type, name, options = {}, &block)
6
+ super(config, type, name, options)
7
7
  @block = block
8
8
  end
9
9
 
@@ -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.5.0'.freeze
2
+ VERSION = '1.8.1'.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.5.0
4
+ version: 1.8.1
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-04-16 00:00:00.000000000 Z
11
+ date: 2021-07-13 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