inline_forms 8.1.75 → 8.1.78

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.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +56 -1
  3. data/README.rdoc +1 -1
  4. data/app/assets/stylesheets/inline_forms/inline_forms.scss +3 -1
  5. data/app/controllers/inline_forms_controller.rb +61 -14
  6. data/app/views/inline_forms/_header.html.erb +1 -1
  7. data/app/views/inline_forms/row_close.html.erb +5 -0
  8. data/inline_forms.gemspec +0 -1
  9. data/lib/generators/USAGE +1 -1
  10. data/lib/generators/inline_forms_addto/USAGE +1 -1
  11. data/lib/generators/inline_forms_addto_generator.rb +30 -139
  12. data/lib/generators/inline_forms_attribute_list_editor.rb +211 -0
  13. data/lib/generators/inline_forms_attribute_overrides.rb +9 -0
  14. data/lib/generators/inline_forms_generator.rb +16 -35
  15. data/lib/inline_forms/form_elements/check_list_helper.rb +22 -6
  16. data/lib/inline_forms/form_elements/dropdown_helper.rb +22 -8
  17. data/lib/inline_forms/gem_files.rb +20 -17
  18. data/lib/inline_forms/refused_values.rb +33 -0
  19. data/lib/inline_forms/version.rb +1 -1
  20. data/lib/inline_forms.rb +10 -0
  21. data/lib/locales/inline_forms.en.yml +2 -0
  22. data/lib/locales/inline_forms.nl.yml +2 -0
  23. metadata +4 -142
  24. data/.document +0 -5
  25. data/.forgejo/brakeman-engine.sh +0 -51
  26. data/.forgejo/brakeman.ignore +0 -211
  27. data/.forgejo/workflows/ci.yml +0 -74
  28. data/.forgejo/workflows/full-gate.yml +0 -104
  29. data/.gitignore +0 -39
  30. data/.rubocop.yml +0 -31
  31. data/.rubocop_todo.yml +0 -151
  32. data/.ruby-version +0 -1
  33. data/Gemfile +0 -28
  34. data/Rakefile +0 -103
  35. data/script/migrate_form_elements.rb +0 -62
  36. data/test/archived_form_elements_test.rb +0 -101
  37. data/test/attribute_list_test.rb +0 -165
  38. data/test/dummy/app/controllers/application_controller.rb +0 -32
  39. data/test/dummy/app/controllers/daily_reports_controller.rb +0 -4
  40. data/test/dummy/app/controllers/documents_controller.rb +0 -4
  41. data/test/dummy/app/controllers/dossiers_controller.rb +0 -4
  42. data/test/dummy/app/controllers/gizmos_controller.rb +0 -4
  43. data/test/dummy/app/controllers/machines_controller.rb +0 -4
  44. data/test/dummy/app/controllers/parts_controller.rb +0 -4
  45. data/test/dummy/app/controllers/stats_controller.rb +0 -10
  46. data/test/dummy/app/controllers/widgets_controller.rb +0 -4
  47. data/test/dummy/app/helpers/application_helper.rb +0 -13
  48. data/test/dummy/app/models/ability.rb +0 -15
  49. data/test/dummy/app/models/application_record.rb +0 -27
  50. data/test/dummy/app/models/daily_report.rb +0 -21
  51. data/test/dummy/app/models/document.rb +0 -33
  52. data/test/dummy/app/models/dossier.rb +0 -38
  53. data/test/dummy/app/models/gizmo.rb +0 -25
  54. data/test/dummy/app/models/kind.rb +0 -8
  55. data/test/dummy/app/models/machine.rb +0 -24
  56. data/test/dummy/app/models/part.rb +0 -17
  57. data/test/dummy/app/models/phase.rb +0 -9
  58. data/test/dummy/app/models/widget.rb +0 -40
  59. data/test/dummy/app/views/_gizmos_search.html.erb +0 -3
  60. data/test/dummy/app/views/stats/show.html.erb +0 -1
  61. data/test/dummy/config/application.rb +0 -49
  62. data/test/dummy/config/database.yml +0 -3
  63. data/test/dummy/config/environment.rb +0 -5
  64. data/test/dummy/config/initializers/inline_forms.rb +0 -6
  65. data/test/dummy/config/initializers/paper_trail_yaml_safe_load.rb +0 -28
  66. data/test/dummy/config/routes.rb +0 -51
  67. data/test/form_element_from_callee_test.rb +0 -73
  68. data/test/gem_package_modes_test.rb +0 -86
  69. data/test/inline_edit_polymorphic_path_test.rb +0 -81
  70. data/test/inline_forms_addto_generator_test.rb +0 -584
  71. data/test/inline_forms_generator_test.rb +0 -301
  72. data/test/integration/csp_nonce_test.rb +0 -50
  73. data/test/integration/dropdown_test.rb +0 -186
  74. data/test/integration/field_authorization_test.rb +0 -131
  75. data/test/integration/file_slots_test.rb +0 -665
  76. data/test/integration/html_refusal_test.rb +0 -152
  77. data/test/integration/inline_forms_crud_test.rb +0 -165
  78. data/test/integration/layout_title_test.rb +0 -56
  79. data/test/integration/legacy_elements_test.rb +0 -99
  80. data/test/integration/list_params_test.rb +0 -200
  81. data/test/integration/locale_test.rb +0 -234
  82. data/test/integration/model_top_bar_test.rb +0 -85
  83. data/test/integration/native_inputs_test.rb +0 -138
  84. data/test/integration/open_after_create_test.rb +0 -85
  85. data/test/integration/pending_migration_gate_test.rb +0 -75
  86. data/test/integration/presentation_escaping_test.rb +0 -80
  87. data/test/integration/revert_skipped_columns_test.rb +0 -59
  88. data/test/integration/schema_batch_pipeline_test.rb +0 -189
  89. data/test/integration/schema_edit_test.rb +0 -141
  90. data/test/integration/schema_preview_test.rb +0 -65
  91. data/test/integration/turbo_menu_test.rb +0 -60
  92. data/test/integration_test_helper.rb +0 -203
  93. data/test/plain_text_configuration_test.rb +0 -90
  94. data/test/schema_apply_test.rb +0 -73
  95. data/test/schema_intent_test.rb +0 -53
  96. data/test/schema_label_test.rb +0 -61
  97. data/test/tabs_test.rb +0 -98
  98. data/test/test_helper.rb +0 -10
  99. data/test/user_model_config_test.rb +0 -44
@@ -0,0 +1,211 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module InlineForms
6
+ # Adds a row to the `@inline_forms_attribute_list ||= [ ... ]` literal in a
7
+ # model's source (used by `rails g inline_forms_addto`).
8
+ #
9
+ # The list is located with Prism, Ruby's own parser, so a row is whatever Ruby
10
+ # says is an element of the array: rows over several lines, several rows on
11
+ # one line, `.freeze`, conditional rows, splats, and brackets or `#` in a
12
+ # label or a comment are all just elements. The result is parsed again before
13
+ # it is returned; what cannot be placed raises Unplaceable and the caller
14
+ # leaves the file alone.
15
+ #
16
+ # Style of what is written (RuboCop omakase): a comma between rows, none after
17
+ # the last one.
18
+ class AttributeListEditor
19
+ class Unplaceable < StandardError; end
20
+
21
+ # attribute_list rows with these names, or with the :info element, start the
22
+ # trailing "metadata" block: new rows go above it by default.
23
+ METADATA_ROW_NAMES = %w[created_at updated_at].freeze
24
+ COMMENTED_ROW = /\A[ \t]*#[ \t]*\[[ \t]*:/
25
+
26
+ def initialize(source)
27
+ @source = source
28
+ @bytes = source.b
29
+ parsed = Prism.parse(source)
30
+ raise Unplaceable, "the file has a syntax error" if parsed.errors.any?
31
+
32
+ @array = find_array(parsed.value)
33
+ @elements = @array ? @array.elements : []
34
+ end
35
+
36
+ # false when the model has no such list, e.g. it inherits one.
37
+ def list?
38
+ !@array.nil?
39
+ end
40
+
41
+ def row?(name)
42
+ !index_of(name).nil?
43
+ end
44
+
45
+ # Returns [ new_source, note ]. +after+ / +before+ name an existing row; the
46
+ # caller checks with row? that it exists.
47
+ def insert(row, after: nil, before: nil)
48
+ raise Unplaceable, "there is no inline_forms_attribute_list" unless list?
49
+
50
+ edits, note = plan(row, after: after, before: before)
51
+ result = apply(edits)
52
+ verify!(result)
53
+ [ result, note ]
54
+ end
55
+
56
+ private
57
+
58
+ def plan(row, after:, before:)
59
+ return [ after_element(index_of(after), row), " (after :#{after})" ] if after
60
+ return [ before_element(index_of(before), row), " (before :#{before})" ] if before
61
+ return [ only_element(row), "" ] if @elements.empty?
62
+ if (metadata = metadata_start_index)
63
+ return [ before_element(metadata, row), " (above metadata)" ]
64
+ end
65
+
66
+ last_row = @elements.rindex { |element| row_symbols(element).any? } || @elements.size - 1
67
+ [ after_element(last_row, row), "" ]
68
+ end
69
+
70
+ # An edit is [ byte offset, text to insert, bytes to replace ].
71
+ def after_element(index, row)
72
+ element_end = @elements[index].location.end_offset
73
+ last = index == @elements.size - 1
74
+ following = last ? @array.closing_loc.start_offset : @elements[index + 1].location.start_offset
75
+ comma_end = comma_end_between(element_end, following)
76
+ position = comma_end || element_end
77
+ comma = comma_end ? "" : ","
78
+ separator = last ? "" : ","
79
+
80
+ line_end = @bytes.index("\n", position)
81
+ return [ [ position, "#{comma} #{row}#{separator}", 0 ] ] if line_end.nil? || line_end >= following
82
+
83
+ edits = comma_end ? [] : [ [ element_end, comma, 0 ] ]
84
+ position = line_end + 1
85
+ # Rows that are commented out below the last one keep the comma they have.
86
+ position = skip_commented_rows(position, following) if last
87
+ edits << [ position, "#{indent}#{row}#{separator}\n", 0 ]
88
+ end
89
+
90
+ def before_element(index, row)
91
+ start = @elements[index].location.start_offset
92
+ lead = line_lead(start)
93
+ return [ [ start - lead.bytesize, "#{lead}#{row},\n", 0 ] ] if lead
94
+
95
+ [ [ start, "#{row}, ", 0 ] ]
96
+ end
97
+
98
+ def only_element(row)
99
+ closing = @array.closing_loc.start_offset
100
+ lead = line_lead(closing)
101
+ return [ [ closing - lead.bytesize, "#{indent}#{row}\n", 0 ] ] if lead
102
+
103
+ opening_end = @array.opening_loc.end_offset
104
+ [ [ opening_end, "\n#{indent}#{row}\n#{base_indent}", closing - opening_end ] ]
105
+ end
106
+
107
+ def apply(edits)
108
+ result = @bytes.dup
109
+ edits.sort_by(&:first).reverse_each do |position, text, length|
110
+ result[position, length] = text.b
111
+ end
112
+ result.force_encoding(@source.encoding)
113
+ end
114
+
115
+ def verify!(result)
116
+ edited = self.class.new(result)
117
+ return if edited.send(:element_count) == @elements.size + 1
118
+
119
+ raise Unplaceable, "the row would not become one element of the list"
120
+ end
121
+
122
+ def element_count
123
+ @elements.size
124
+ end
125
+
126
+ def find_array(node)
127
+ queue = [ node ]
128
+ until queue.empty?
129
+ current = queue.shift
130
+ if current.is_a?(Prism::InstanceVariableOrWriteNode) && current.name == :@inline_forms_attribute_list
131
+ array = leftmost_array(current.value)
132
+ return array if array
133
+ end
134
+ queue.concat(current.compact_child_nodes)
135
+ end
136
+ nil
137
+ end
138
+
139
+ # The array literal in `[ ... ]`, `[ ... ].freeze` or `[ ... ] + MORE`.
140
+ def leftmost_array(node)
141
+ node = node.receiver while node.is_a?(Prism::CallNode)
142
+ node if node.is_a?(Prism::ArrayNode) && node.opening_loc&.slice == "["
143
+ end
144
+
145
+ # [ name, form element ] of a row as strings; empty for anything else.
146
+ def row_symbols(element)
147
+ array = leftmost_array(element)
148
+ return [] unless array
149
+
150
+ symbols = array.elements.first(2)
151
+ return [] unless symbols.first.is_a?(Prism::SymbolNode)
152
+
153
+ symbols.map { |node| node.unescaped if node.is_a?(Prism::SymbolNode) }
154
+ end
155
+
156
+ def index_of(name)
157
+ @elements.index { |element| row_symbols(element).first == name.to_s }
158
+ end
159
+
160
+ # A `:header` right above the first metadata row belongs to the block.
161
+ def metadata_start_index
162
+ first = @elements.index do |element|
163
+ name, form_element = row_symbols(element)
164
+ form_element == "info" || METADATA_ROW_NAMES.include?(name)
165
+ end
166
+ return nil unless first
167
+
168
+ first.positive? && row_symbols(@elements[first - 1])[1] == "header" ? first - 1 : first
169
+ end
170
+
171
+ # Between two elements there are only commas, whitespace and comments, but a
172
+ # comment may contain a comma: ask the lexer.
173
+ def comma_end_between(from, to)
174
+ @commas ||= Prism.lex(@source).value.filter_map do |token, _state|
175
+ token.location if token.type == :COMMA
176
+ end
177
+ @commas.find { |comma| comma.start_offset >= from && comma.start_offset < to }&.end_offset
178
+ end
179
+
180
+ def skip_commented_rows(position, limit)
181
+ while position < limit
182
+ line_end = @bytes.index("\n", position)
183
+ break unless line_end && line_end < limit && @bytes[position..line_end].match?(COMMENTED_ROW)
184
+
185
+ position = line_end + 1
186
+ end
187
+ position
188
+ end
189
+
190
+ # The whitespace before +offset+ when nothing else is on its line before it.
191
+ def line_lead(offset)
192
+ newline = offset.zero? ? nil : @bytes.rindex("\n", offset - 1)
193
+ line_start = newline ? newline + 1 : 0
194
+ lead = @bytes[line_start...offset]
195
+ lead if lead.match?(/\A[ \t]*\z/)
196
+ end
197
+
198
+ def indent
199
+ @elements.each do |element|
200
+ lead = line_lead(element.location.start_offset)
201
+ return lead if lead
202
+ end
203
+ "#{base_indent} "
204
+ end
205
+
206
+ def base_indent
207
+ newline = @bytes.rindex("\n", @array.opening_loc.start_offset)
208
+ @bytes[(newline ? newline + 1 : 0)..][/\A[ \t]*/]
209
+ end
210
+ end
211
+ end
@@ -105,6 +105,15 @@ Rails::Generators::GeneratedAttribute.class_eval do
105
105
  _no_model
106
106
  ].freeze unless const_defined?(:SPECIAL_GENERATOR_NAMES)
107
107
 
108
+ # Precision and scale of a decimal column, from `name:decimal_field{p,s}` or
109
+ # the 10,2 default. Without them MySQL makes it decimal(10,0) and drops the
110
+ # decimals. Empty for every other type.
111
+ def inline_forms_column_options
112
+ return "" unless column_type == :decimal
113
+
114
+ ", precision: #{attr_options[:precision] || 10}, scale: #{attr_options[:scale] || 2}"
115
+ end
116
+
108
117
  def migration?
109
118
  not ( column_type == :no_migration ||
110
119
  SPECIAL_GENERATOR_NAMES.include?(name))
@@ -127,17 +127,18 @@ module InlineForms
127
127
  @list_scopes << " inline_forms_search_on :#{attribute.type}\n"
128
128
  end
129
129
  if attribute.attribute?
130
- commenter = attribute.attribute_type == :unknown ? '#' : ' '
131
- rows << "#{commenter} [ :#{attribute.name}, :#{attribute.attribute_type} ]"
130
+ commenter = attribute.attribute_type == :unknown ? '# ' : ''
131
+ rows << " #{commenter}[ :#{attribute.name}, :#{attribute.attribute_type} ]"
132
132
  end
133
133
  end
134
134
  unless rows.empty?
135
- # No comma after the last row that is not commented out (RuboCop
136
- # omakase); inline_forms_addto adds it when it appends a row.
137
- last_row = rows.rindex { |row| !row.start_with?('#') }
138
- rows.each_with_index do |row, index|
139
- @inline_forms_attribute_list << row << (index == last_row ? "\n" : ",\n")
140
- end
135
+ # No comma after the last row (RuboCop omakase); inline_forms_addto adds
136
+ # it when it appends one. Commented-out rows at the end go above that
137
+ # last row, so that uncommenting one gives valid Ruby.
138
+ last_row = rows.rindex { |row| !row.lstrip.start_with?('#') }
139
+ last_row = rows.delete_at(last_row) if last_row
140
+ rows.each { |row| @inline_forms_attribute_list << row << ",\n" }
141
+ @inline_forms_attribute_list << last_row << "\n" if last_row
141
142
  @inline_forms_attribute_list = "\n" +
142
143
  " def inline_forms_attribute_list\n" +
143
144
  " @inline_forms_attribute_list ||= [\n" +
@@ -179,31 +180,9 @@ module InlineForms
179
180
  @columns << ' t.datetime :' + attribute.name + "_updated_at\n"
180
181
  else
181
182
  if attribute.migration?
182
- attribute.attribute_type == :unknown ? commenter = '#' : commenter = ' '
183
- # Render precision/scale for real :decimal columns. The override
184
- # in lib/generators/inline_forms_attribute_overrides.rb teaches
185
- # `Rails::Generators::GeneratedAttribute.parse` to extract
186
- # `{p,s}` off the type for :decimal_field (Rails' own parser
187
- # only honors `{…}` for a hardcoded list of built-in types
188
- # and otherwise glues the braces onto the type symbol).
189
- # Apply sensible defaults — precision: 10, scale: 2 — so a
190
- # bare `name:decimal_field` becomes a real decimal column
191
- # instead of the legacy varchar that 8.1.9 and earlier
192
- # emitted. Existing apps keep their varchar columns until
193
- # they migrate; only newly-generated tables get :decimal.
194
- column_opts = ""
195
- if attribute.column_type == :decimal
196
- precision = attribute.attr_options[:precision] || 10
197
- scale = attribute.attr_options[:scale] || 2
198
- column_opts = ", precision: #{precision}, scale: #{scale}"
199
- end
200
- @columns << commenter +
201
- ' t.' +
202
- attribute.column_type.to_s +
203
- " :" +
204
- attribute.name +
205
- column_opts +
206
- "\n"
183
+ commenter = attribute.attribute_type == :unknown ? '# ' : ''
184
+ @columns << " #{commenter}t.#{attribute.column_type} :#{attribute.name}" \
185
+ "#{attribute.inline_forms_column_options}\n"
207
186
  end
208
187
  end
209
188
  end
@@ -232,9 +211,11 @@ module InlineForms
232
211
 
233
212
  content = File.read(full_path)
234
213
  return unless content.match?(marker)
235
- return if content.include?(tab_token.rstrip)
214
+ # One of the words, not a substring: "showcases" is not there because
215
+ # "form_element_showcases" is. force: Thor skips on a substring too.
216
+ return if content[/MODEL_TABS = %w[\[(]([^\])]*)/, 1].to_s.split.include?(tab_token.rstrip)
236
217
 
237
- inject_into_file relative_path, tab_token, after: marker
218
+ inject_into_file relative_path, tab_token, after: marker, force: true
238
219
  end
239
220
 
240
221
  def generate_test
@@ -20,11 +20,7 @@ module InlineForms
20
20
  def check_list_edit(object, attribute)
21
21
  object.send(attribute).build if object.send(attribute).empty?
22
22
  klass = object.send(attribute).first.class
23
- values = cancan_enabled? ? klass.accessible_by(current_ability) : klass.all
24
- # check_list has always applied the list scope's filter (a where in
25
- # inline_forms_list hides records here); the order is DropdownOrder's.
26
- values = values.merge(klass.inline_forms_list.unscope(:order)) if klass.respond_to?(:inline_forms_list)
27
- values = InlineForms::DropdownOrder.apply(values)
23
+ values = InlineForms::DropdownOrder.apply(check_list_offered(klass))
28
24
  out = ''
29
25
  values.each do | item |
30
26
  out << "<div class='row #{cycle('odd', 'even')}'>"
@@ -35,9 +31,29 @@ module InlineForms
35
31
  out.html_safe
36
32
  end
37
33
 
34
+ # The records the check list offers; check_list_update accepts no others.
35
+ def check_list_offered(klass)
36
+ values = cancan_enabled? ? klass.accessible_by(current_ability) : klass.all
37
+ # check_list has always applied the list scope's filter (a where in
38
+ # inline_forms_list hides records here); the order is DropdownOrder's.
39
+ values = values.merge(klass.inline_forms_list.unscope(:order)) if klass.respond_to?(:inline_forms_list)
40
+ values
41
+ end
42
+
38
43
  def check_list_update(object, attribute)
39
44
  params[attribute] ||= {}
40
- object.send(attribute.to_s.singularize + '_ids=', params[attribute].keys)
45
+ ids = attribute.to_s.singularize + '_ids'
46
+ posted = params[attribute].keys.map(&:to_s)
47
+ held = object.send(ids).map(&:to_s)
48
+ offered = check_list_offered(object.class.reflect_on_association(attribute.to_sym).klass).map { |item| item.id.to_s }
49
+ # A hidden record is also a refused one. Nothing is assigned: on a saved
50
+ # record that would write the join rows at once.
51
+ if (posted - offered - held).any?
52
+ InlineForms::RefusedValues.add(object, attribute)
53
+ return
54
+ end
55
+ # Records the user was not shown stay: not ticked is not unticked.
56
+ object.send(ids + '=', posted | (held - offered))
41
57
  object.touch unless object.new_record? # Check for new_record needed for Rails > 3; TODO we should have a flag to turn this on or of.
42
58
  end
43
59
  INLINE_FORMS_FORM_ELEMENT
@@ -20,12 +20,7 @@ module InlineForms
20
20
  presentation = "_presentation"
21
21
  presentation = "_dropdown_presentation" if attr.respond_to? "_dropdown_presentation"
22
22
  klass = object.send(attribute).class
23
- if cancan_enabled?
24
- values = klass.accessible_by(current_ability)
25
- else
26
- values = klass.all
27
- end
28
- values = InlineForms::DropdownOrder.apply(values)
23
+ values = InlineForms::DropdownOrder.apply(dropdown_offered(klass))
29
24
  options_disabled = nil
30
25
  if klass.method_defined? :disabled_for_dropdown?
31
26
  # loads values; collection_select below reuses the loaded records
@@ -42,6 +37,17 @@ module InlineForms
42
37
  )
43
38
  end
44
39
 
40
+ # The records the dropdown offers; dropdown_update accepts no others.
41
+ def dropdown_offered(klass)
42
+ cancan_enabled? ? klass.accessible_by(current_ability) : klass.all
43
+ end
44
+
45
+ # An option shown disabled (disabled_for_dropdown?) is not offered either.
46
+ def dropdown_offers?(klass, value)
47
+ record = dropdown_offered(klass).find_by(klass.primary_key => value.to_s)
48
+ record.present? && !(record.respond_to?(:disabled_for_dropdown?) && record.disabled_for_dropdown?)
49
+ end
50
+
45
51
  def dropdown_update(object, attribute)
46
52
  # Inline edit posts the value under the leading-underscore wrapper key
47
53
  # (`params[:_apartment][:owner_id]`); top-level create through a
@@ -51,8 +57,16 @@ module InlineForms
51
57
  # walks do not raise NoMethodError on nil[:owner_id].
52
58
  scope = params[('_' + object.class.to_s.underscore).to_sym]
53
59
  return if scope.blank?
54
- foreign_key = object.class.reflect_on_association(attribute.to_sym).options[:foreign_key] || attribute.to_s.foreign_key.to_sym
55
- object[foreign_key] = scope[attribute.to_s.foreign_key.to_sym]
60
+ reflection = object.class.reflect_on_association(attribute.to_sym)
61
+ foreign_key = reflection.options[:foreign_key] || attribute.to_s.foreign_key.to_sym
62
+ value = scope[attribute.to_s.foreign_key.to_sym]
63
+ # A hidden option is also a refused value. The value the record already
64
+ # has may stay. It is assigned all the same, so that a model that refuses
65
+ # it says so in its own words; RefusedValues keeps it from being saved.
66
+ if value.present? && value.to_s != object[foreign_key].to_s && !dropdown_offers?(reflection.klass, value)
67
+ InlineForms::RefusedValues.add(object, attribute)
68
+ end
69
+ object[foreign_key] = value
56
70
  end
57
71
 
58
72
  def dropdown_info(object, attribute)
@@ -19,21 +19,24 @@ module InlineFormsGemFiles
19
19
  inline_forms_schema_edit/
20
20
  ].freeze
21
21
 
22
- # Scratch / local-notes dir. It holds working notes and, crucially, secrets
23
- # such as stuff/forgejo-token (mode 0600, only used by CI pushes to forgejo).
24
- # It must NEVER be packaged into a gem. Do not rely on git ignore for this:
25
- # gem_files sweeps untracked files too, and the global excludes file that
26
- # ignores stuff/ is per-machine (present here, absent on the release box), so
27
- # the token leaked into `gem build` on the release machine. Exclude it hard,
28
- # independent of any ignore configuration.
29
- #
30
- # Same for Claude Code files: .claude/ (untracked, per checkout:
31
- # launch.json, settings.local.json, worktrees) and CLAUDE.md (committed so
32
- # worktrees see it, but repo instructions, not gem content).
33
- EXCLUDED_FILE_PREFIXES = %w[
34
- stuff/
35
- .claude/
36
- CLAUDE.md
22
+ # What an installed gem needs: the engine, the archived form elements (hosts
23
+ # copy them; ArchivedFormElements points there) and the documents. Everything
24
+ # else in the checkout stays out: tests, CI, RuboCop and Ruby version files, and
25
+ # above all untracked files, which gem_files sweeps too. stuff/ holds working notes and secrets such as stuff/forgejo-token; the
26
+ # global excludes file that ignores it is per-machine and absent on the release
27
+ # box, where the token once leaked into `gem build`. Hence a list of what goes
28
+ # in, not of what stays out.
29
+ PACKAGED_PREFIXES = %w[
30
+ app/
31
+ archived/
32
+ bin/
33
+ lib/
34
+ vendor/assets/
35
+ CHANGELOG.md
36
+ LICENSE.txt
37
+ README.rdoc
38
+ inline_forms.gemspec
39
+ inline_forms_installer.gemspec
37
40
  ].freeze
38
41
 
39
42
  module_function
@@ -59,8 +62,8 @@ module InlineFormsGemFiles
59
62
 
60
63
  files.select! { |f| File.file?(File.join(REPO_ROOT, f)) }
61
64
 
62
- files.reject! do |f|
63
- EXCLUDED_FILE_PREFIXES.any? { |prefix| f == prefix || f.start_with?(prefix) }
65
+ files.select! do |f|
66
+ PACKAGED_PREFIXES.any? { |prefix| prefix.end_with?("/") ? f.start_with?(prefix) : f == prefix }
64
67
  end
65
68
 
66
69
  files.reject! do |f|
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InlineForms
4
+ # A field got a value it did not offer (a dropdown or check_list id outside
5
+ # the records the user may read). The record stays invalid while that value
6
+ # is on it, whoever saves it: the engine's create / update, or a host's own.
7
+ #
8
+ # The model's own validations run first, so a model that refuses the value
9
+ # with a message of its own keeps that message; :inclusion is only added
10
+ # when the attribute, or its foreign key, has no error yet.
11
+ module RefusedValues
12
+ def self.add(object, attribute)
13
+ object.extend(self)
14
+ object.inline_forms_refused_values << attribute.to_sym
15
+ end
16
+
17
+ def inline_forms_refused_values
18
+ @inline_forms_refused_values ||= Set.new
19
+ end
20
+
21
+ def valid?(...)
22
+ super
23
+ inline_forms_refused_values.each do |attribute|
24
+ foreign_key = self.class.reflect_on_association(attribute)&.foreign_key
25
+ next if errors.include?(attribute) || (foreign_key && errors.include?(foreign_key.to_sym))
26
+
27
+ errors.add(attribute, :inclusion)
28
+ end
29
+ errors.empty?
30
+ end
31
+ alias_method :validate, :valid?
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module InlineForms
4
- VERSION = "8.1.75"
4
+ VERSION = "8.1.78"
5
5
  end
data/lib/inline_forms.rb CHANGED
@@ -11,6 +11,7 @@ require "inline_forms/schema_label"
11
11
  require "inline_forms/form_element_from_callee"
12
12
  require "inline_forms/searchable"
13
13
  require "inline_forms/dropdown_order"
14
+ require "inline_forms/refused_values"
14
15
  require "inline_forms/archived_form_elements"
15
16
  require "inline_forms/form_element_registry"
16
17
  require "inline_forms/form_elements"
@@ -65,6 +66,15 @@ module InlineForms
65
66
  end
66
67
  end
67
68
 
69
+ # Is this user the superadmin? An app that has its own answer
70
+ # (`User#superadmin?`, e.g. only user 1) is asked first; otherwise it is
71
+ # whoever holds the role. A user class with neither is not a superadmin.
72
+ def self.superadmin?(user)
73
+ return !!user.superadmin? if user.respond_to?(:superadmin?)
74
+
75
+ user.respond_to?(:role?) && !!user.role?(:superadmin)
76
+ end
77
+
68
78
  # The cross-record trash page (superadmin). One line in routes.rb:
69
79
  # InlineForms.draw_file_trash_routes(self)
70
80
  def self.draw_file_trash_routes(router)
@@ -34,6 +34,8 @@ en:
34
34
  close: close
35
35
  add_new: make a new %{model}
36
36
  create_failed: Could not create %{model}.
37
+ destroy_failed: Could not delete %{model}.
38
+ restore_failed: Could not restore %{model}.
37
39
  undo: undo
38
40
  restore: restore
39
41
  trash: trash
@@ -34,6 +34,8 @@ nl:
34
34
  close: sluiten
35
35
  add_new: Maak een nieuw %{model}
36
36
  create_failed: Kan %{model} niet aanmaken.
37
+ destroy_failed: Kan %{model} niet verwijderen.
38
+ restore_failed: Kan %{model} niet terugzetten.
37
39
  undo: terughalen
38
40
  restore: terugzetten
39
41
  trash: weggooien