lifeform 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db20cd6cdb2581c3943d46e325a07a30ac89581cb6d29c1fd8297aab9663f202
4
+ data.tar.gz: ccd569f076822e5df6eecee26021b4a2f7044d5d91721f6b3523c6944bb64bfa
5
+ SHA512:
6
+ metadata.gz: 6d71ebf92fb482bdb9d34bb8ace8f1fae6625a191d6595fc73c0b144b11ab94f8d3352b2c03d43b9f4de64c9c67bace0ba6f40898d8165592132786dd25757b5
7
+ data.tar.gz: 800dbc481b4860d53c71b420cdb28da5b32d9781aecbe901ed75e1b8bfc36d1c6b626d504bfa377b68ceaff58158b47cead59406be31a1beb3a88fafd1cfbe17
data/.rubocop.yml ADDED
@@ -0,0 +1,38 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: enable
4
+
5
+ Metrics/AbcSize:
6
+ Exclude:
7
+ - test/**/*.rb
8
+
9
+ Metrics/MethodLength:
10
+ Max: 20
11
+ Exclude:
12
+ - test/**/*.rb
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/Lambda:
18
+ Enabled: false
19
+
20
+ Style/LambdaCall:
21
+ Enabled: false
22
+
23
+ Style/MultilineBlockChain:
24
+ Enabled: false
25
+
26
+ Style/StringLiterals:
27
+ Enabled: true
28
+ EnforcedStyle: double_quotes
29
+
30
+ Style/StringLiteralsInInterpolation:
31
+ Enabled: true
32
+ EnforcedStyle: double_quotes
33
+
34
+ Style/ParallelAssignment:
35
+ Enabled: false
36
+
37
+ Layout/LineLength:
38
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [0.1.0] - 2022-05-29
6
+
7
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jared@jaredwhite.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in lifeform.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "solargraph", "~> 0.45.0"
15
+
16
+ gem "rails-dom-testing", "~> 2.0"
17
+
18
+ gem "rubocop-minitest", "~> 0.20.0"
19
+ gem "rubocop-rake", "~> 0.6.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,108 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lifeform (0.1.0)
5
+ activesupport (>= 6.0)
6
+ papercraft (~> 0.24)
7
+ zeitwerk (~> 2.5)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.0.3)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ ast (2.4.2)
18
+ backport (1.2.0)
19
+ benchmark (0.2.0)
20
+ concurrent-ruby (1.1.10)
21
+ diff-lcs (1.5.0)
22
+ e2mmap (0.1.0)
23
+ escape_utils (1.2.1)
24
+ i18n (1.10.0)
25
+ concurrent-ruby (~> 1.0)
26
+ jaro_winkler (1.5.4)
27
+ kramdown (2.3.2)
28
+ rexml
29
+ kramdown-parser-gfm (1.1.0)
30
+ kramdown (~> 2.0)
31
+ minitest (5.15.0)
32
+ nokogiri (1.13.6-arm64-darwin)
33
+ racc (~> 1.4)
34
+ papercraft (0.24)
35
+ escape_utils (~> 1.2.1)
36
+ kramdown (~> 2.3.1)
37
+ kramdown-parser-gfm (~> 1.1.0)
38
+ rouge (~> 3.27.0)
39
+ parallel (1.22.1)
40
+ parser (3.1.2.0)
41
+ ast (~> 2.4.1)
42
+ racc (1.6.0)
43
+ rails-dom-testing (2.0.3)
44
+ activesupport (>= 4.2.0)
45
+ nokogiri (>= 1.6)
46
+ rainbow (3.1.1)
47
+ rake (13.0.6)
48
+ regexp_parser (2.5.0)
49
+ reverse_markdown (2.1.1)
50
+ nokogiri
51
+ rexml (3.2.5)
52
+ rouge (3.27.0)
53
+ rubocop (1.30.0)
54
+ parallel (~> 1.10)
55
+ parser (>= 3.1.0.0)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ regexp_parser (>= 1.8, < 3.0)
58
+ rexml (>= 3.2.5, < 4.0)
59
+ rubocop-ast (>= 1.18.0, < 2.0)
60
+ ruby-progressbar (~> 1.7)
61
+ unicode-display_width (>= 1.4.0, < 3.0)
62
+ rubocop-ast (1.18.0)
63
+ parser (>= 3.1.1.0)
64
+ rubocop-minitest (0.20.0)
65
+ rubocop (>= 0.90, < 2.0)
66
+ rubocop-rake (0.6.0)
67
+ rubocop (~> 1.0)
68
+ ruby-progressbar (1.11.0)
69
+ solargraph (0.45.0)
70
+ backport (~> 1.2)
71
+ benchmark
72
+ bundler (>= 1.17.2)
73
+ diff-lcs (~> 1.4)
74
+ e2mmap
75
+ jaro_winkler (~> 1.5)
76
+ kramdown (~> 2.3)
77
+ kramdown-parser-gfm (~> 1.1)
78
+ parser (~> 3.0)
79
+ reverse_markdown (>= 1.0.5, < 3)
80
+ rubocop (>= 0.52)
81
+ thor (~> 1.0)
82
+ tilt (~> 2.0)
83
+ yard (~> 0.9, >= 0.9.24)
84
+ thor (1.2.1)
85
+ tilt (2.0.10)
86
+ tzinfo (2.0.4)
87
+ concurrent-ruby (~> 1.0)
88
+ unicode-display_width (2.1.0)
89
+ webrick (1.7.0)
90
+ yard (0.9.27)
91
+ webrick (~> 1.7.0)
92
+ zeitwerk (2.5.4)
93
+
94
+ PLATFORMS
95
+ arm64-darwin-21
96
+
97
+ DEPENDENCIES
98
+ lifeform!
99
+ minitest (~> 5.0)
100
+ rails-dom-testing (~> 2.0)
101
+ rake (~> 13.0)
102
+ rubocop (~> 1.21)
103
+ rubocop-minitest (~> 0.20.0)
104
+ rubocop-rake (~> 0.6.0)
105
+ solargraph (~> 0.45.0)
106
+
107
+ BUNDLED WITH
108
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Jared White and Contributors
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,33 @@
1
+ # Lifeform
2
+
3
+ Component-centric form object rendering for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add Lifeform to your application's Gemfile by running:
8
+
9
+ ```sh
10
+ $ bundle add lifeform
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ TODO: Write usage instructions here
16
+
17
+ ## Development
18
+
19
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ 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 `bin/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).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bridgetownrb/lifeform. 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/bridgetownrb/lifeform/blob/main/CODE_OF_CONDUCT.md).
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
30
+
31
+ ## Code of Conduct
32
+
33
+ Everyone interacting in the Lifeform project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bridgetownrb/lifeform/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.warning = false
10
+ t.test_files = FileList["test/**/test_*.rb"]
11
+ end
12
+
13
+ require "rubocop/rake_task"
14
+
15
+ RuboCop::RakeTask.new
16
+
17
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "lifeform"
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("bundle", __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/rubocop ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("bundle", __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
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,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/string/inflections"
4
+
5
+ module Lifeform
6
+ FieldDefinition = Struct.new(:type, :library, :parameters)
7
+
8
+ # A form object which stores field definitions and can be rendered as a component
9
+ class Form
10
+ MODEL_PATH_HELPER = :polymorphic_path
11
+
12
+ class << self
13
+ # Helper to point to `I18n.t` method
14
+ def t(...)
15
+ I18n.t(...)
16
+ end
17
+
18
+ # @return [Hash<Symbol, FieldDefinition>]
19
+ def fields
20
+ @fields ||= {}
21
+ end
22
+
23
+ def field(name, type: :text, library: self.library, **parameters)
24
+ parameters[:name] = name
25
+ fields[name] = FieldDefinition.new(type, Libraries.const_get(library.to_s.classify), parameters)
26
+ end
27
+
28
+ def library(library_name = nil)
29
+ @library ||= library_name || :default
30
+ end
31
+
32
+ def escape_value(value)
33
+ case value
34
+ when TrueClass, FalseClass
35
+ value
36
+ else
37
+ EscapeUtils.escape_html(value.to_s)
38
+ end
39
+ end
40
+
41
+ # @param kwargs [Hash]
42
+ def parameters_to_attributes(kwargs)
43
+ previous_value = EscapeUtils.html_secure
44
+ EscapeUtils.html_secure = false
45
+
46
+ attributes = {}
47
+ kwargs.each do |key, value|
48
+ case value
49
+ when Hash
50
+ value.each do |inner_key, inner_value|
51
+ attributes[:"#{key}_#{inner_key}"] = escape_value(inner_value)
52
+ end
53
+ else
54
+ attributes[key] = escape_value(value) unless value.nil?
55
+ end
56
+ end
57
+
58
+ EscapeUtils.html_secure = previous_value
59
+
60
+ attributes
61
+ end
62
+ end
63
+
64
+ # @return [Object]
65
+ attr_reader :model
66
+
67
+ # @return [String]
68
+ attr_reader :url
69
+
70
+ # @return [Class<Libraries::Default>]
71
+ attr_reader :library
72
+
73
+ # @return [Hash]
74
+ attr_reader :parameters
75
+
76
+ # @return [Boolean]
77
+ attr_reader :emit_form_tag
78
+
79
+ def initialize(model = nil, url: nil, library: self.class.library, emit_form_tag: true, **parameters)
80
+ @model, @url, @library_name, @parameters, @emit_form_tag = model, url, library, parameters, emit_form_tag
81
+ @library = Libraries.const_get(@library_name.to_s.classify)
82
+
83
+ parameters[:method] ||= model.respond_to?(:persisted?) && model.persisted? ? :patch : :post
84
+ parameters[:accept_charset] ||= "UTF-8"
85
+ verify_method
86
+ end
87
+
88
+ def verify_method
89
+ return if %w[get post].include?(parameters[:method].to_s.downcase)
90
+
91
+ @method_tag = Papercraft.html do |method_name|
92
+ input type: "hidden", name: "_method", value: method_name, autocomplete: "off"
93
+ end.render(parameters[:method].to_s.downcase)
94
+ parameters[:method] = :post
95
+ end
96
+
97
+ def add_authenticity_token(view_context) # rubocop:disable Metrics/AbcSize
98
+ if view_context.respond_to?(:token_tag, true) # Rails
99
+ view_context.send(:token_tag, nil, form_options: {
100
+ action: parameters[:action].to_s,
101
+ method: parameters[:method].to_s.downcase
102
+ })
103
+ elsif view_context.respond_to?(:csrf_tag, true) # Roda
104
+ view_context.send(:csrf_tag, action: parameters[:action].to_s, method: parameters[:method].to_s)
105
+ else
106
+ raise Lifeform::Error, "Missing token tag helper. Override `add_authenticity_token' in your Form object"
107
+ end
108
+ end
109
+
110
+ def attributes
111
+ @attributes ||= self.class.parameters_to_attributes(parameters)
112
+ end
113
+
114
+ def field(name, **field_parameters)
115
+ field_definition = self.class.fields[name]
116
+ field_definition.library.object_for_field_definition(
117
+ self, field_definition, self.class.parameters_to_attributes(field_parameters)
118
+ )
119
+ end
120
+
121
+ def render_in(view_context, &block) # rubocop:disable Metrics/AbcSize
122
+ form_tag = library::FORM_TAG
123
+ parameters[:action] ||= url || (model ? view_context.send(self.class.const_get(:MODEL_PATH_HELPER), model) : nil)
124
+
125
+ content = view_context.capture(self, &block)
126
+
127
+ return content unless emit_form_tag
128
+
129
+ content = add_authenticity_token(view_context) + content.to_s unless parameters[:method].to_s.downcase == "get"
130
+ content = @method_tag + content.to_s if @method_tag
131
+
132
+ # if @hidden_submit_button
133
+ # content += %(\n<button type="submit" style="position: absolute; left: -9999px"></button>).html_safe
134
+ # end
135
+
136
+ Papercraft.html do |attr|
137
+ send(form_tag, **attr) { emit content }
138
+ end.render(attributes)
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lifeform
4
+ module Libraries
5
+ class Default
6
+ class Input
7
+ attr_reader :form, :field_definition, :attributes
8
+
9
+ INPUT_TAG = :input
10
+ WRAPPER_TAG = :form_field
11
+
12
+ def initialize(form, field_definition, **attributes)
13
+ @form = form
14
+ @field_definition = field_definition
15
+ @attributes = Lifeform::Form.parameters_to_attributes(field_definition.parameters).merge(attributes)
16
+ @field_type = field_definition.type
17
+
18
+ verify_attributes
19
+ end
20
+
21
+ def verify_attributes # rubocop:disable Metrics
22
+ @model = attributes.delete(:model)
23
+ @model = form.model if form.model && @model.nil?
24
+
25
+ attributes[:value] ||= value_for_model if form.model
26
+ attributes[:name] = "#{model_name}[#{attributes[:name]}]" if @model
27
+ attributes[:id] ||= attributes[:name].parameterize(separator: "_")
28
+ @label = handle_labels if attributes[:label]
29
+ end
30
+
31
+ def model_name
32
+ return @model.to_model.model_name.param_key if @model.respond_to?(:to_model)
33
+
34
+ # Or just use basic underscore
35
+ @model.class.name.underscore.tr("/", "_")
36
+ end
37
+
38
+ def value_for_model
39
+ @model.send(attributes[:name])
40
+ end
41
+
42
+ def handle_labels
43
+ Papercraft.html do |label_text, name|
44
+ label label_text, for: name
45
+ end.render(
46
+ attributes[:label].to_s,
47
+ (attributes[:id] || attributes[:name]).to_s
48
+ ).tap do
49
+ @attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h
50
+ end
51
+ end
52
+
53
+ def render_in(view_context)
54
+ @view_context = view_context
55
+ template
56
+ end
57
+
58
+ def template
59
+ Papercraft.html do |wrapper_tag:, input_tag:, attributes:, field_type:, field_label:|
60
+ field_body = proc {
61
+ emit(field_label) if field_label
62
+ send input_tag, type: field_type, **attributes
63
+ }
64
+ next field_body.call unless wrapper_tag
65
+
66
+ send wrapper_tag, name: attributes[:name], &field_body
67
+ end.render(
68
+ wrapper_tag: self.class.const_get(:WRAPPER_TAG),
69
+ input_tag: self.class.const_get(:INPUT_TAG),
70
+ attributes: attributes,
71
+ field_type: @field_type,
72
+ field_label: @label
73
+ )
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lifeform
4
+ module Libraries
5
+ class Default
6
+ FORM_TAG = :form
7
+
8
+ # @return [Input]
9
+ def self.object_for_field_definition(form, field_definition, attributes)
10
+ type_classname = field_definition[:type].to_s.classify
11
+ if const_defined?(type_classname)
12
+ const_get(type_classname)
13
+ else
14
+ const_get(:Input)
15
+ end.new(form, field_definition, **attributes)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lifeform
4
+ module Libraries
5
+ class Shoelace
6
+ class Input < Default::Input
7
+ INPUT_TAG = :sl_input
8
+
9
+ # no-op
10
+ def handle_labels; end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lifeform
4
+ module Libraries
5
+ class Shoelace < Default
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lifeform
4
+ VERSION = "0.1.0"
5
+ end
data/lib/lifeform.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "papercraft"
4
+
5
+ require "zeitwerk"
6
+ loader = Zeitwerk::Loader.for_gem
7
+ loader.setup
8
+
9
+ module Lifeform
10
+ class Error < StandardError; end
11
+ end
data/lifeform.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/lifeform/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lifeform"
7
+ spec.version = Lifeform::VERSION
8
+ spec.author = "Bridgetown Team"
9
+ spec.email = "maintainers@bridgetownrb.com"
10
+
11
+ spec.summary = "Component-centric form object rendering for Ruby"
12
+ spec.homepage = "https://github.com/bridgetownrb/lifeform"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.7"
15
+ spec.metadata["rubygems_mfa_required"] = "true"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
22
+ end
23
+ end
24
+ spec.require_paths = ["lib"]
25
+
26
+ # Uncomment to register a new dependency of your gem
27
+ spec.add_dependency "activesupport", ">= 6.0"
28
+ spec.add_dependency "papercraft", "~> 0.24"
29
+ spec.add_dependency "zeitwerk", "~> 2.5"
30
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lifeform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bridgetown Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: papercraft
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.24'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.24'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
55
+ description:
56
+ email: maintainers@bridgetownrb.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rubocop.yml"
62
+ - ".ruby-version"
63
+ - CHANGELOG.md
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/rake
72
+ - bin/rubocop
73
+ - bin/setup
74
+ - lib/lifeform.rb
75
+ - lib/lifeform/form.rb
76
+ - lib/lifeform/libraries/default.rb
77
+ - lib/lifeform/libraries/default/input.rb
78
+ - lib/lifeform/libraries/shoelace.rb
79
+ - lib/lifeform/libraries/shoelace/input.rb
80
+ - lib/lifeform/version.rb
81
+ - lifeform.gemspec
82
+ homepage: https://github.com/bridgetownrb/lifeform
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ rubygems_mfa_required: 'true'
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '2.7'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.3.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Component-centric form object rendering for Ruby
106
+ test_files: []