lifeform 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db20cd6cdb2581c3943d46e325a07a30ac89581cb6d29c1fd8297aab9663f202
4
- data.tar.gz: ccd569f076822e5df6eecee26021b4a2f7044d5d91721f6b3523c6944bb64bfa
3
+ metadata.gz: c1a951565aa5729c703d08032dc10a135d57f39be110ea7a77f1daea226ea7b5
4
+ data.tar.gz: b6411dc03979e7e69b30c51f99861565323ec39a7687148a3b4705a5ef4040f9
5
5
  SHA512:
6
- metadata.gz: 6d71ebf92fb482bdb9d34bb8ace8f1fae6625a191d6595fc73c0b144b11ab94f8d3352b2c03d43b9f4de64c9c67bace0ba6f40898d8165592132786dd25757b5
7
- data.tar.gz: 800dbc481b4860d53c71b420cdb28da5b32d9781aecbe901ed75e1b8bfc36d1c6b626d504bfa377b68ceaff58158b47cead59406be31a1beb3a88fafd1cfbe17
6
+ metadata.gz: fb6e4f3177e0cc95c89ed7fe898ce977abbdfd755e53e6706edaf3c362508b56944f779f49bb0f82f4ce3f4f79817e4056352e15c2fbe7382defee52b2568037
7
+ data.tar.gz: 2fd2aa7715782c21847cbaa5fac8ac6e692711a944ad6fe627c2df378380677d5ddd6b62429c7f073103a8750d5af7093ef2b6664b490f8f67d6594ecc1e82d6
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.2.0] - 2022-05-31
6
+
7
+ - Fields now support content blocks, conditional rendering
8
+ - Form subclass will now inherit parent form library
9
+
5
10
  ## [0.1.0] - 2022-05-29
6
11
 
7
12
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lifeform (0.1.0)
4
+ lifeform (0.2.0)
5
5
  activesupport (>= 6.0)
6
6
  papercraft (~> 0.24)
7
7
  zeitwerk (~> 2.5)
@@ -31,6 +31,8 @@ GEM
31
31
  minitest (5.15.0)
32
32
  nokogiri (1.13.6-arm64-darwin)
33
33
  racc (~> 1.4)
34
+ nokogiri (1.13.6-x86_64-linux)
35
+ racc (~> 1.4)
34
36
  papercraft (0.24)
35
37
  escape_utils (~> 1.2.1)
36
38
  kramdown (~> 2.3.1)
@@ -93,6 +95,7 @@ GEM
93
95
 
94
96
  PLATFORMS
95
97
  arm64-darwin-21
98
+ x86_64-linux
96
99
 
97
100
  DEPENDENCIES
98
101
  lifeform!
data/README.md CHANGED
@@ -12,7 +12,44 @@ $ bundle add lifeform
12
12
 
13
13
  ## Usage
14
14
 
15
- TODO: Write usage instructions here
15
+ Full documentation coming as the library begins to mature. TL;DR:
16
+
17
+ Given a form object of:
18
+
19
+ ```rb
20
+ class TestForm < Lifeform::Form
21
+ field :occupation, label: "Your Job", id: "your-occupation", required: true
22
+ field :age, library: :shoelace, label: "Your Age"
23
+ end
24
+ ```
25
+
26
+ And a template rendering of:
27
+
28
+ ```erb
29
+ <%= render TestForm.new(url: "/path") do %>
30
+ <%= render f.field(:occupation) %>
31
+ <%= render f.field(:age, value: 47) %>
32
+ <% end %>
33
+ ```
34
+
35
+ You get the following HTML output:
36
+
37
+ ```html
38
+ <form method="post" accept-charset="UTF-8" action="/path">
39
+ <input type="hidden" name="authenticity_token" value="[token]" />
40
+ <form-field name="occupation">
41
+ <label for="your-occupation">Your Job</label>
42
+ <input type="text" id="your-occupation" required name="occupation" />
43
+ </form-field>
44
+ <form-field name="age">
45
+ <sl-input type="text" label="Your Age" name="age" value="47" id="age"></sl-input>
46
+ </form-field>
47
+ </form>
48
+ ```
49
+
50
+ Nested names based on models (aka `profile[name]`) and inferred action paths are supported as well.
51
+
52
+ Multiple component libraries and input types—and easy customizability via [Papercraft](https://github.com/digital-fabric/papercraft) templates—are a fundamental aspect of the architecture of Lifeform.
16
53
 
17
54
  ## Development
18
55
 
data/lib/lifeform/form.rb CHANGED
@@ -10,6 +10,11 @@ module Lifeform
10
10
  MODEL_PATH_HELPER = :polymorphic_path
11
11
 
12
12
  class << self
13
+ def inherited(subclass)
14
+ super
15
+ subclass.library library
16
+ end
17
+
13
18
  # Helper to point to `I18n.t` method
14
19
  def t(...)
15
20
  I18n.t(...)
@@ -26,7 +31,8 @@ module Lifeform
26
31
  end
27
32
 
28
33
  def library(library_name = nil)
29
- @library ||= library_name || :default
34
+ @library = library_name if library_name
35
+ @library ||= :default
30
36
  end
31
37
 
32
38
  def escape_value(value)
@@ -112,8 +118,11 @@ module Lifeform
112
118
  end
113
119
 
114
120
  def field(name, **field_parameters)
121
+ # @type [FieldDefinition]
115
122
  field_definition = self.class.fields[name]
116
- field_definition.library.object_for_field_definition(
123
+ # @type [Class<Libraries::Default>]
124
+ field_library = field_definition.library
125
+ field_library.object_for_field_definition(
117
126
  self, field_definition, self.class.parameters_to_attributes(field_parameters)
118
127
  )
119
128
  end
@@ -22,6 +22,7 @@ module Lifeform
22
22
  @model = attributes.delete(:model)
23
23
  @model = form.model if form.model && @model.nil?
24
24
 
25
+ @if = attributes.delete(:if)
25
26
  attributes[:value] ||= value_for_model if form.model
26
27
  attributes[:name] = "#{model_name}[#{attributes[:name]}]" if @model
27
28
  attributes[:id] ||= attributes[:name].parameterize(separator: "_")
@@ -50,16 +51,20 @@ module Lifeform
50
51
  end
51
52
  end
52
53
 
53
- def render_in(view_context)
54
+ def render_in(view_context, &block)
54
55
  @view_context = view_context
56
+ @content = block
57
+ return "" if !@if.nil? && !@if
58
+
55
59
  template
56
60
  end
57
61
 
58
- def template
59
- Papercraft.html do |wrapper_tag:, input_tag:, attributes:, field_type:, field_label:|
62
+ def template # rubocop:disable Metrics/AbcSize
63
+ Papercraft.html do |wrapper_tag:, input_tag:, attributes:, field_data:|
60
64
  field_body = proc {
61
- emit(field_label) if field_label
62
- send input_tag, type: field_type, **attributes
65
+ emit(field_data[:label]) if field_data[:label]
66
+ send input_tag, type: field_data[:type], **attributes
67
+ emit(field_data[:content]) if field_data[:content]
63
68
  }
64
69
  next field_body.call unless wrapper_tag
65
70
 
@@ -68,8 +73,11 @@ module Lifeform
68
73
  wrapper_tag: self.class.const_get(:WRAPPER_TAG),
69
74
  input_tag: self.class.const_get(:INPUT_TAG),
70
75
  attributes: attributes,
71
- field_type: @field_type,
72
- field_label: @label
76
+ field_data: {
77
+ type: @field_type,
78
+ label: @label,
79
+ content: @content && @view_context.capture(&@content)
80
+ }
73
81
  )
74
82
  end
75
83
  end
@@ -5,6 +5,9 @@ module Lifeform
5
5
  class Default
6
6
  FORM_TAG = :form
7
7
 
8
+ # @param form [LifeForm::Form]
9
+ # @param field_definition [LifeForm::FieldDefinition]
10
+ # @param attributes [Hash]
8
11
  # @return [Input]
9
12
  def self.object_for_field_definition(form, field_definition, attributes)
10
13
  type_classname = field_definition[:type].to_s.classify
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lifeform
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lifeform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-30 00:00:00.000000000 Z
11
+ date: 2022-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport