dynamic_layouts 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.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +8 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/Gemfile +12 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +39 -0
  10. data/Rakefile +12 -0
  11. data/bin/console +11 -0
  12. data/bin/setup +8 -0
  13. data/dynamic_layouts.gemspec +31 -0
  14. data/lib/dynamic_layouts/dynamic_layout_builder.rb +45 -0
  15. data/lib/dynamic_layouts/dynamic_layout_service.rb +217 -0
  16. data/lib/dynamic_layouts/dynamic_layout_transformer.rb +10 -0
  17. data/lib/dynamic_layouts/version.rb +5 -0
  18. data/lib/dynamic_layouts.rb +10 -0
  19. data/lib/generators/dynamic_layouts/install/install_generator.rb +46 -0
  20. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_containers.rb +12 -0
  21. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_dashboard_widgets.rb +13 -0
  22. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_grid_fields.rb +19 -0
  23. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_markdown_elements.rb +11 -0
  24. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_sections.rb +18 -0
  25. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_table_columns.rb +15 -0
  26. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_table_configs.rb +13 -0
  27. data/lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layouts.rb +9 -0
  28. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout.rb +35 -0
  29. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_container.rb +15 -0
  30. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_dashboard_widget.rb +12 -0
  31. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_grid_field.rb +21 -0
  32. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_markdown_element.rb +10 -0
  33. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_section.rb +55 -0
  34. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_table_column.rb +17 -0
  35. data/lib/generators/dynamic_layouts/templates/models/dynamic_layout_table_config.rb +13 -0
  36. data/sig/dynamic_layouts.rbs +4 -0
  37. data/spec/dynamic_layouts_spec.rb +7 -0
  38. data/spec/spec_helper.rb +15 -0
  39. metadata +99 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e0b9e03aa572a8f7d1109b8a5b712d00fbd9c6b9ece7dd130d95a69043919de
4
+ data.tar.gz: e595057c5c95c165a2823e666c4d9bd6ed5eace6308bbe6c310b5ffe7404bf20
5
+ SHA512:
6
+ metadata.gz: 283631224c1c912cdda718d4ab769c2a87960619dac6177660d5dcc3ecb8e9fb2634947277cce17bf717821a5ab18e5ef251389cb7ec0bb43223559afab1aa3f
7
+ data.tar.gz: 6bead906043a49945de821c163b09c38cbee7b07231bd87fe56972a5aa247624eee1211e225a3f5f327edbd4d8d21edc5a25e4f50fec11505134e24db6618fff
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.2
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-09-01
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in dynamic_layouts.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DynamicLayouts
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dynamic_layouts`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dynamic_layouts. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dynamic_layouts/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the DynamicLayouts project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dynamic_layouts/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dynamic_layouts"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/dynamic_layouts/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dynamic_layouts"
7
+ spec.version = DynamicLayouts::VERSION
8
+ spec.authors = ["Mark Harbison"]
9
+ spec.email = ["mark@tyne-solutions.com"]
10
+
11
+ spec.summary = "Dynamic Layouts"
12
+ spec.description = "Dynamic Layouts"
13
+ spec.homepage = "https://github.com/CodeTectonics/dynamic-layouts"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.2.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
20
+
21
+ spec.files = `git ls-files`.split("\n")
22
+ spec.test_files = `git ls-files -- spec/*`.split("\n")
23
+ spec.require_paths = ["lib"]
24
+
25
+ # Uncomment to register a new dependency of your gem
26
+ # spec.add_dependency "example-gem", "~> 1.0"
27
+ spec.add_dependency "rails", ">= 6.1"
28
+
29
+ # For more information and examples about making a new gem, check out our
30
+ # guide at: https://bundler.io/guides/creating_gem.html
31
+ end
@@ -0,0 +1,45 @@
1
+ module DynamicLayouts
2
+ class DynamicLayoutBuilder
3
+ attr_accessor :dynamic_layout
4
+
5
+ def initialize(name)
6
+ self.dynamic_layout = DynamicLayout.find_by name: name
7
+ end
8
+
9
+ def eager_load_sections
10
+ dynamic_layout.eager_load_sections
11
+ self
12
+ end
13
+
14
+ def serialize
15
+ self.dynamic_layout = dynamic_layout.serialize
16
+ self
17
+ end
18
+
19
+ def transform(record: nil, controller: nil, user: nil, transformer_class: nil)
20
+ transformer_class ||= implied_transformer_class(controller)
21
+
22
+ if transformer_class.present?
23
+ transformer_class.new(dynamic_layout, record, controller, user).transform
24
+ end
25
+
26
+ self
27
+ end
28
+
29
+ def implied_transformer_class(controller)
30
+ return nil if controller.nil?
31
+
32
+ "#{controller.controller_name.classify}LayoutTransformer".constantize
33
+ rescue NameError
34
+ nil
35
+ end
36
+
37
+ def self.build(name, **transform_params)
38
+ DynamicLayoutBuilder.new(name)
39
+ .eager_load_sections
40
+ .serialize
41
+ .transform(**transform_params)
42
+ .dynamic_layout
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,217 @@
1
+ module DynamicLayouts
2
+ class DynamicLayoutService
3
+ def self.override_layout_config(dynamic_layout, overrides)
4
+ dynamic_layout[:containers].each do |container|
5
+ container[:sections].each do |section|
6
+ section_overrides = overrides[section[:slug].to_sym]
7
+ next if section_overrides.blank?
8
+
9
+ if section_overrides.keys.excluding(:elements).any?
10
+ override_section_config(section, section_overrides)
11
+ end
12
+
13
+ next if section_overrides[:elements].blank?
14
+
15
+ case section[:layout]
16
+ when 'table', 'roster-shift-list'
17
+ override_table_section_element_configs(section, section_overrides)
18
+ override_grid_section_element_configs(section[:config][:modal_section], section_overrides)
19
+ when 'dashboard'
20
+ override_dashboard_section_element_configs(section, section_overrides)
21
+ else
22
+ override_grid_section_element_configs(section, section_overrides)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.override_section_config(section, section_overrides)
29
+ section[:writable] = section_overrides[:writable] if section_overrides.key?(:writable)
30
+ section[:hidden] = section_overrides[:hidden] if section_overrides.key?(:hidden)
31
+ section[:collapsed] = section_overrides[:collapsed] if section_overrides.key?(:collapsed)
32
+ end
33
+
34
+ def self.override_table_section_element_configs(section, section_overrides)
35
+ section[:elements].each do |table_column|
36
+ field_overrides = section_overrides[:elements][table_column[:attr].to_sym]
37
+ next if field_overrides.blank?
38
+
39
+ table_column[:header] = field_overrides[:label] if field_overrides.key?(:label)
40
+ table_column[:hidden] = field_overrides[:hidden] if field_overrides.key?(:hidden)
41
+ end
42
+ end
43
+
44
+ def self.override_dashboard_section_element_configs(section, section_overrides)
45
+ section[:elements].each do |dashboard_widget|
46
+ field_overrides = section_overrides[:elements][dashboard_widget[:slug].to_sym]
47
+ next if field_overrides.blank?
48
+
49
+ dashboard_widget[:label] = field_overrides[:label] if field_overrides.key?(:label)
50
+ dashboard_widget[:hidden] = field_overrides[:hidden] if field_overrides.key?(:hidden)
51
+ end
52
+ end
53
+
54
+ def self.override_grid_section_element_configs(section, section_overrides)
55
+ section[:elements].each do |grid_field|
56
+ field_overrides = section_overrides[:elements][grid_field[:attr].to_sym]
57
+ next if field_overrides.blank?
58
+
59
+ grid_field[:label] = field_overrides[:label] if field_overrides.key?(:label)
60
+ grid_field[:required] = field_overrides[:required] if field_overrides.key?(:required)
61
+ grid_field[:disabled] = field_overrides[:disabled] if field_overrides.key?(:disabled)
62
+ grid_field[:hidden] = field_overrides[:hidden] if field_overrides.key?(:hidden)
63
+ grid_field[:default] = field_overrides[:default] if field_overrides.key?(:default)
64
+ grid_field[:tooltip] = field_overrides[:tooltip] if field_overrides.key?(:tooltip)
65
+
66
+ next unless field_overrides.key?(:options)
67
+
68
+ grid_field[:options].merge!(field_overrides[:options])
69
+ end
70
+ end
71
+
72
+ def self.serialize_options(options, attr_type = nil)
73
+ return {} if options.blank?
74
+
75
+ serialized_options = hashify_options(options)
76
+
77
+ case attr_type
78
+ when 'dropdown', 'dropdown-md', 'dropdown-lg', 'multiselect', 'multiselect-md', 'multiselect-lg'
79
+ serialize_dropdown_field_values(serialized_options)
80
+ when 'autocomplete'
81
+ serialize_autocomplete_field_values(serialized_options)
82
+ when 'rich-text'
83
+ serialize_rich_text_field_values(serialized_options)
84
+ when 'report-config'
85
+ serialize_report_config(serialized_options)
86
+ when 'chart-config'
87
+ serialize_chart_config(serialized_options)
88
+ end
89
+
90
+ serialized_options
91
+ end
92
+
93
+ def self.hashify_options(options_string)
94
+ options_hash = {}
95
+ options_string.split(';').each do |option|
96
+ key, val = option.strip.split(':')
97
+ options_hash[key] = val
98
+ end
99
+ options_hash.symbolize_keys!
100
+ end
101
+
102
+ def self.serialize_search_values(search_values)
103
+ search_values.each do |search_attr, options|
104
+ search_values[search_attr] = serialize_dropdown_field_values(options)
105
+ end
106
+ end
107
+
108
+ def self.serialize_dropdown_field_values(options)
109
+ case options[:mode]
110
+ when 'static'
111
+ options[:values] = serialize_static_dropdown_field_values(options)
112
+ when 'association'
113
+ options[:values] = serialize_association_dropdown_field_values(options)
114
+ options = options.except(:class, :scopes, :label, :value, :hidden)
115
+ end
116
+
117
+ options[:values].each { |i| i[:label] = I18n.t(i[:label], default: i[:label]) }
118
+
119
+ if options[:disable_sort]
120
+ options[:values]
121
+ else
122
+ options[:values].sort_by! { |i| i[:label] || '' }
123
+ end
124
+ end
125
+
126
+ def self.serialize_static_dropdown_field_values(options)
127
+ options[:values].split(',').map do |item|
128
+ if item.include?('=')
129
+ value, label = item.split('=')
130
+ else
131
+ value = label = item
132
+ end
133
+ { value: value.strip, label: label.strip }
134
+ end
135
+ end
136
+
137
+ def self.serialize_association_dropdown_field_values(options)
138
+ data_source = options[:scopes].split(',').inject(options[:class].constantize) do |res, scope|
139
+ res.send(scope)
140
+ end
141
+
142
+ data_source.map do |item|
143
+ if item.class.respond_to?(:decorator_class?) && item.class.decorator_class?
144
+ item = item.decorate
145
+ end
146
+
147
+ result = { label: item.send(options[:label]), value: item.send(options[:value]).to_s }
148
+ result[:hidden] = item.send(options[:hidden]) if options[:hidden].present?
149
+ result
150
+ end
151
+ end
152
+
153
+ def self.serialize_autocomplete_field_values(options)
154
+ case options[:mode]
155
+ when 'static'
156
+ options[:values] = serialize_static_autocomplete_field_values(options)
157
+ when 'historic'
158
+ options[:values] = serialize_historic_autocomplete_field_values(options)
159
+ options = options.except(:class, :scopes, :attribute)
160
+ end
161
+
162
+ options[:values].sort_by! { |i| i[:label] || '' }
163
+ end
164
+
165
+ def self.serialize_static_autocomplete_field_values(options)
166
+ options[:values].split(',').map do |item|
167
+ item.strip!
168
+ { label: item, value: item }
169
+ end
170
+ end
171
+
172
+ def self.serialize_historic_autocomplete_field_values(options)
173
+ data_source = options[:scopes].split(',').inject(options[:class].constantize) do |res, scope|
174
+ res.send(scope)
175
+ end
176
+
177
+ data_source.where("#{options[:attribute]} IS NOT NULL AND #{options[:attribute]} != ''")
178
+ .select("LOWER(TRIM(#{options[:attribute]})) AS formatted_value")
179
+ .order(:formatted_value).distinct
180
+ .map do |item|
181
+ { label: item.formatted_value.capitalize, value: item.formatted_value.capitalize }
182
+ end
183
+ end
184
+
185
+ def self.serialize_rich_text_field_values(options)
186
+ options[:placeholder_attrs] = options[:placeholder_attrs].split(',').map do |item|
187
+ item.strip!
188
+ { key: item, value: item }
189
+ end
190
+ options[:placeholder_attrs].sort_by! { |i| i[:value] || '' }
191
+ end
192
+
193
+ def self.serialize_report_config(options)
194
+ dataset_slugs = options[:datasets].split(',').map(&:strip)
195
+ datasets = Dataset.where(slug: dataset_slugs)
196
+ options[:datasets] = datasets.map do |dataset|
197
+ {
198
+ id: dataset.id,
199
+ name: dataset.name,
200
+ columns: AnalyticsPlane::DataSources::Registry.fetch(dataset.slug).column_name_and_types
201
+ }
202
+ end
203
+ end
204
+
205
+ def self.serialize_chart_config(options)
206
+ dataset_slugs = options[:datasets].split(',').map(&:strip)
207
+ datasets = Dataset.where(slug: dataset_slugs)
208
+ options[:datasets] = datasets.map do |dataset|
209
+ {
210
+ id: dataset.id,
211
+ name: dataset.name,
212
+ columns: AnalyticsPlane::DataSources::Registry.fetch(dataset.slug).column_name_and_types
213
+ }
214
+ end
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,10 @@
1
+ module DynamicLayouts
2
+ class DynamicLayoutTransformer
3
+ def initialize(dynamic_layout, record, controller, current_user)
4
+ @dynamic_layout = dynamic_layout
5
+ @record = record
6
+ @controller = controller
7
+ @current_user = current_user
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DynamicLayouts
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dynamic_layouts/dynamic_layout_builder"
4
+ require_relative "dynamic_layouts/dynamic_layout_service"
5
+ require_relative "dynamic_layouts/dynamic_layout_transformer"
6
+ require_relative "dynamic_layouts/version"
7
+
8
+ module DynamicLayouts
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,46 @@
1
+ require "rails/generators"
2
+ require "rails/generators/named_base"
3
+ require "rails/generators/migration"
4
+
5
+ module DynamicLayouts
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.expand_path("../templates", __dir__)
11
+
12
+ desc "Creates the models and migrations used by a Dynamic Layout."
13
+
14
+ def create_models
15
+ template "models/dynamic_layout.rb", "app/models/dynamic_layout.rb"
16
+ template "models/dynamic_layout_container.rb", "app/models/dynamic_layout_container.rb"
17
+ template "models/dynamic_layout_section.rb", "app/models/dynamic_layout_section.rb"
18
+ template "models/dynamic_layout_grid_field.rb", "app/models/dynamic_layout_grid_field.rb"
19
+ template "models/dynamic_layout_table_column.rb", "app/models/dynamic_layout_table_column.rb"
20
+ template "models/dynamic_layout_table_config.rb", "app/models/dynamic_layout_table_config.rb"
21
+ template "models/dynamic_layout_markdown_element.rb", "app/models/dynamic_layout_markdown_element.rb"
22
+ template "models/dynamic_layout_dashboard_widget.rb", "app/models/dynamic_layout_dashboard_widget.rb"
23
+ end
24
+
25
+ def create_migrations
26
+ migration_template "migrations/create_dynamic_layouts.rb", "db/migrate/create_dynamic_layouts.rb"
27
+ migration_template "migrations/create_dynamic_layout_containers.rb", "db/migrate/create_dynamic_layout_containers.rb"
28
+ migration_template "migrations/create_dynamic_layout_sections.rb", "db/migrate/create_dynamic_layout_sections.rb"
29
+ migration_template "migrations/create_dynamic_layout_grid_fields.rb", "db/migrate/create_dynamic_layout_grid_fields.rb"
30
+ migration_template "migrations/create_dynamic_layout_table_columns.rb", "db/migrate/create_dynamic_layout_table_columns.rb"
31
+ migration_template "migrations/create_dynamic_layout_table_configs.rb", "db/migrate/create_dynamic_layout_table_configs.rb"
32
+ migration_template "migrations/create_dynamic_layout_markdown_elements.rb", "db/migrate/create_dynamic_layout_markdown_elements.rb"
33
+ migration_template "migrations/create_dynamic_layout_dashboard_widgets.rb", "db/migrate/create_dynamic_layout_dashboard_widgets.rb"
34
+ end
35
+
36
+ def self.next_migration_number(dirname)
37
+ if @next_migration_number
38
+ @next_migration_number += 1
39
+ else
40
+ @next_migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
41
+ end
42
+ @next_migration_number.to_s
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ class CreateDynamicLayoutContainers < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_containers do |t|
4
+ t.references :dynamic_layout, foreign_key: true
5
+ t.string :slug
6
+ t.string :container_style
7
+ t.integer :ordinal, default: 0
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class CreateDynamicLayoutDashboardWidgets < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_dashboard_widgets do |t|
4
+ t.references :dynamic_layout_section, foreign_key: true, index: { name: :index_dynamic_layout_dashboard_widgets_on_section_id }
5
+ t.string :label
6
+ t.string :widget_slug
7
+ t.integer :ordinal, default: 0
8
+ t.boolean :hidden, default: false, null: false
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ class CreateDynamicLayoutGridFields < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_grid_fields do |t|
4
+ t.references :dynamic_layout_section, foreign_key: true
5
+ t.string :label
6
+ t.string :attr_name
7
+ t.string :attr_type
8
+ t.string :options, limit: 2000
9
+ t.string :default
10
+ t.boolean :required, default: false, null: false
11
+ t.boolean :disabled, default: false, null: false
12
+ t.boolean :hidden, default: false, null: false
13
+ t.integer :ordinal, default: 0
14
+ t.string :tooltip
15
+
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ class CreateDynamicLayoutMarkdownElements < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_markdown_elements do |t|
4
+ t.references :dynamic_layout_section, foreign_key: true, index: { name: :index_dynamic_layout_markdown_elements_on_section_id }
5
+ t.text :text
6
+ t.integer :ordinal, default: 0
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ class CreateDynamicLayoutSections < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_sections do |t|
4
+ t.references :dynamic_layout_container, foreign_key: true
5
+ t.string :title
6
+ t.string :slug
7
+ t.string :layout_style
8
+ t.references :config, polymorphic: true
9
+ t.boolean :writable, default: true, null: false
10
+ t.boolean :hidden, default: false, null: false
11
+ t.boolean :collapsed, default: false, null: false
12
+ t.integer :ordinal, default: 0
13
+ t.string :options, limit: 2000
14
+
15
+ t.timestamps
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ class CreateDynamicLayoutTableColumns < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_table_columns do |t|
4
+ t.references :dynamic_layout_section, foreign_key: true
5
+ t.string :label
6
+ t.string :attr_name
7
+ t.string :attr_type
8
+ t.string :options, limit: 2000
9
+ t.boolean :hidden, default: false, null: false
10
+ t.integer :ordinal, default: 0
11
+
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class CreateDynamicLayoutTableConfigs < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layout_table_configs do |t|
4
+ t.string :attr_name
5
+ t.boolean :can_add, default: true, null: false
6
+ t.boolean :can_edit, default: true, null: false
7
+ t.boolean :can_delete, default: true, null: false
8
+ t.references :modal_section, foreign_key: { to_table: :dynamic_layout_sections }
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ class CreateDynamicLayouts < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :dynamic_layouts do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ class DynamicLayout < ApplicationRecord
2
+ has_many :dynamic_layout_custom_schemas, dependent: :destroy
3
+ has_many :dynamic_layout_containers, -> { order(:ordinal) }, inverse_of: :dynamic_layout,
4
+ dependent: :destroy
5
+ has_many :dynamic_layout_sections, through: :dynamic_layout_containers
6
+ has_many :dynamic_layout_grid_fields, through: :dynamic_layout_sections
7
+ has_many :dynamic_layout_dashboard_widgets, through: :dynamic_layout_sections
8
+ has_many :dynamic_layout_markdown_elements, through: :dynamic_layout_sections
9
+ has_many :dynamic_layout_table_columns, through: :dynamic_layout_sections
10
+
11
+ accepts_nested_attributes_for :dynamic_layout_containers, allow_destroy: true
12
+
13
+ def eager_load_sections
14
+ self.dynamic_layout_containers =
15
+ dynamic_layout_containers.includes(
16
+ dynamic_layout_sections: [
17
+ :dynamic_layout_grid_fields,
18
+ :dynamic_layout_table_columns,
19
+ :dynamic_layout_dashboard_widgets,
20
+ :dynamic_layout_markdown_elements,
21
+ config: [
22
+ modal_section: :dynamic_layout_grid_fields
23
+ ]
24
+ ]
25
+ )
26
+ self
27
+ end
28
+
29
+ def serialize
30
+ {
31
+ name: name,
32
+ containers: dynamic_layout_containers.map(&:serialize)
33
+ }
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ class DynamicLayoutContainer < ApplicationRecord
2
+ belongs_to :dynamic_layout
3
+ has_many :dynamic_layout_sections, -> { order(:ordinal) }, inverse_of: :dynamic_layout_container,
4
+ dependent: :destroy
5
+
6
+ accepts_nested_attributes_for :dynamic_layout_sections, allow_destroy: true
7
+
8
+ def serialize
9
+ {
10
+ slug: slug,
11
+ container_style: container_style,
12
+ sections: dynamic_layout_sections.map(&:serialize)
13
+ }
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ class DynamicLayoutDashboardWidget < ApplicationRecord
2
+ belongs_to :dynamic_layout_section
3
+
4
+ validates :label, presence: true
5
+
6
+ def serialize
7
+ {
8
+ label: label,
9
+ slug: widget_slug
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ class DynamicLayoutGridField < ApplicationRecord
2
+ belongs_to :dynamic_layout_section
3
+
4
+ validates :attr_name, presence: true
5
+ validates :attr_type, presence: true
6
+ validates :label, presence: true
7
+
8
+ def serialize
9
+ {
10
+ label: label,
11
+ tooltip: tooltip,
12
+ attr: attr_name,
13
+ type: attr_type,
14
+ options: DynamicLayouts::DynamicLayoutService.serialize_options(options, attr_type),
15
+ default: default,
16
+ required: required,
17
+ disabled: disabled,
18
+ hidden: hidden
19
+ }
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ class DynamicLayoutMarkdownElement < ApplicationRecord
2
+ belongs_to :dynamic_layout_section
3
+
4
+ def serialize
5
+ {
6
+ text: text,
7
+ ordinal: ordinal
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,55 @@
1
+ class DynamicLayoutSection < ApplicationRecord
2
+ belongs_to :dynamic_layout_container, optional: true
3
+ belongs_to :config, polymorphic: true, optional: true
4
+ has_many :dynamic_layout_grid_fields, -> { order(:ordinal) },
5
+ inverse_of: :dynamic_layout_section, dependent: :destroy
6
+ has_many :dynamic_layout_dashboard_widgets, -> { order(:ordinal) },
7
+ inverse_of: :dynamic_layout_section, dependent: :destroy
8
+ has_many :dynamic_layout_markdown_elements, -> { order(:ordinal) },
9
+ inverse_of: :dynamic_layout_section, dependent: :destroy
10
+ has_many :dynamic_layout_table_columns, -> { order(:ordinal) },
11
+ inverse_of: :dynamic_layout_section, dependent: :destroy
12
+
13
+ accepts_nested_attributes_for :dynamic_layout_grid_fields, allow_destroy: true
14
+ accepts_nested_attributes_for :dynamic_layout_dashboard_widgets, allow_destroy: true
15
+ accepts_nested_attributes_for :dynamic_layout_markdown_elements, allow_destroy: true
16
+ accepts_nested_attributes_for :dynamic_layout_table_columns, allow_destroy: true
17
+
18
+ before_save :ensure_config_exists
19
+
20
+ def serialize
21
+ result = {
22
+ title: title,
23
+ slug: slug,
24
+ layout: layout_style,
25
+ config: config.try(:serialize),
26
+ elements: [],
27
+ writable: writable,
28
+ hidden: hidden,
29
+ collapsed: collapsed,
30
+ options: DynamicLayouts::DynamicLayoutService.serialize_options(options)
31
+ }
32
+
33
+ case layout_style
34
+ when 'grid', 'report'
35
+ result[:elements] = dynamic_layout_grid_fields.map(&:serialize)
36
+ when 'table', 'roster-shift-list'
37
+ result[:elements] = dynamic_layout_table_columns.map(&:serialize)
38
+ when 'dashboard'
39
+ result[:elements] = dynamic_layout_dashboard_widgets.map(&:serialize)
40
+ when 'markdown'
41
+ result[:elements] = dynamic_layout_markdown_elements.map(&:serialize)
42
+ end
43
+
44
+ result
45
+ end
46
+
47
+ private
48
+
49
+ def ensure_config_exists
50
+ return unless layout_style.in? %w[table roster-shift-list board]
51
+ return if config.is_a? DynamicLayoutTableConfig
52
+
53
+ self.config = DynamicLayoutTableConfig.create
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ class DynamicLayoutTableColumn < ApplicationRecord
2
+ belongs_to :dynamic_layout_section
3
+
4
+ validates :attr_name, presence: true
5
+ validates :attr_type, presence: true
6
+ validates :label, presence: true
7
+
8
+ def serialize
9
+ {
10
+ header: label,
11
+ attr: attr_name,
12
+ type: attr_type,
13
+ options: DynamicLayouts::DynamicLayoutService.serialize_options(options, attr_type),
14
+ hidden: hidden
15
+ }
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ class DynamicLayoutTableConfig < ApplicationRecord
2
+ belongs_to :modal_section, class_name: 'DynamicLayoutSection', optional: true
3
+
4
+ def serialize
5
+ {
6
+ attr: attr_name,
7
+ modal_section: modal_section.try(:serialize),
8
+ can_add: can_add,
9
+ can_edit: can_edit,
10
+ can_delete: can_delete
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ module DynamicLayouts
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe DynamicLayouts do
4
+ it "has a version number" do
5
+ expect(DynamicLayouts::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dynamic_layouts"
4
+
5
+ RSpec.configure do |config|
6
+ # Enable flags like --only-failures and --next-failure
7
+ config.example_status_persistence_file_path = ".rspec_status"
8
+
9
+ # Disable RSpec exposing methods globally on `Module` and `main`
10
+ config.disable_monkey_patching!
11
+
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynamic_layouts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Harbison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ description: Dynamic Layouts
28
+ email:
29
+ - mark@tyne-solutions.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - dynamic_layouts.gemspec
46
+ - lib/dynamic_layouts.rb
47
+ - lib/dynamic_layouts/dynamic_layout_builder.rb
48
+ - lib/dynamic_layouts/dynamic_layout_service.rb
49
+ - lib/dynamic_layouts/dynamic_layout_transformer.rb
50
+ - lib/dynamic_layouts/version.rb
51
+ - lib/generators/dynamic_layouts/install/install_generator.rb
52
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_containers.rb
53
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_dashboard_widgets.rb
54
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_grid_fields.rb
55
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_markdown_elements.rb
56
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_sections.rb
57
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_table_columns.rb
58
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layout_table_configs.rb
59
+ - lib/generators/dynamic_layouts/templates/migrations/create_dynamic_layouts.rb
60
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout.rb
61
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_container.rb
62
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_dashboard_widget.rb
63
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_grid_field.rb
64
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_markdown_element.rb
65
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_section.rb
66
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_table_column.rb
67
+ - lib/generators/dynamic_layouts/templates/models/dynamic_layout_table_config.rb
68
+ - sig/dynamic_layouts.rbs
69
+ - spec/dynamic_layouts_spec.rb
70
+ - spec/spec_helper.rb
71
+ homepage: https://github.com/CodeTectonics/dynamic-layouts
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ homepage_uri: https://github.com/CodeTectonics/dynamic-layouts
76
+ source_code_uri: https://github.com/CodeTectonics/dynamic-layouts
77
+ changelog_uri: https://github.com/CodeTectonics/dynamic-layouts/blob/main/CHANGELOG.md
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.2.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.4.19
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Dynamic Layouts
97
+ test_files:
98
+ - spec/dynamic_layouts_spec.rb
99
+ - spec/spec_helper.rb