formwandler 0.7.1 → 0.8.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: 88fd867f333bb338b58f553689d7d244d4c327ddf9a2317124c9b34111e8bd8b
4
- data.tar.gz: b7b4925c4dc382c2de8201bf6d1b4d7b8e4693f284ca7ba89b269629d5d6785a
3
+ metadata.gz: d83c20395db6d499d42e57b1575f9c3020392221358d7ff1f7c825e667dc6ef5
4
+ data.tar.gz: 315035887242f76e2f51336f09b58bb376b597932a505c539d4b0da8fdfb8b58
5
5
  SHA512:
6
- metadata.gz: 497f8c620f1b04ae8789eb6591e9e8689ce0cc44a5fc78762e9895a45befa2b552fac6cd6e97069a7ed3ba268a0dfaf9af3cae628d5862f62ad00d6a1d61c18f
7
- data.tar.gz: 640cb7933dd6614c70b8bdb3196ef724a4bec0679e2f62b80207a02c5992da2786861ec4e553d4471bcc1e9e6ea18454bf3726b7e54b2b1b9e0142a2612c5b33
6
+ metadata.gz: e406427a31ea2cdf5cc8059ff9854e596831bd0ffc0cd9cdd128791ec4624213c3903cb98b403d90bbc88ca147b3bfe21087237c1f11f9fb8ef60b14f378a9b6
7
+ data.tar.gz: 0216f9859f581c28439d671b90935a0158a2b31e487aa008c92673f2c30a1dcd153af047afebd0485baf97b15e9dff054f582787e179d6ec3b5168f380a69642
@@ -5,13 +5,41 @@ require_relative 'field_value_transformer'
5
5
  module Formwandler
6
6
  class FieldDefinition
7
7
  attr_reader :name
8
- attr_accessor :hidden, :disabled, :readonly, :default, :delocalize, :model, :source, :options, :array
8
+ attr_accessor :hidden, :disabled, :readonly, :default, :delocalize, :model, :model_class, :source, :options, :array
9
9
 
10
10
  def initialize(name)
11
11
  @name = name
12
12
  set_defaults
13
13
  end
14
14
 
15
+ def model=(model)
16
+ set_model model, model
17
+ end
18
+
19
+ def model_class=(symbol_or_class)
20
+ @model_class =
21
+ if symbol_or_class.nil?
22
+ nil
23
+ elsif symbol_or_class.is_a?(Symbol)
24
+ symbol_or_class.to_s.classify.constantize rescue nil
25
+ elsif symbol_or_class < ActiveRecord::Base || symbol_or_class < ActiveModel::Model
26
+ symbol_or_class
27
+ else
28
+ fail 'The model class must be nil, a symbol or a descendant of ActiveModel::Model or ActiveRecord::Base.'
29
+ end
30
+ end
31
+
32
+ def configure(opts)
33
+ model = opts[:model]
34
+ model_class = opts[:model_class] || model
35
+ set_model model, model_class
36
+ opts.except(:model, :model_class).each do |key, value|
37
+ send("#{key}=", value)
38
+ rescue NoMethodError
39
+ raise ArgumentError, "Invalid option #{key}"
40
+ end
41
+ end
42
+
15
43
  def hide_option(name, value = true)
16
44
  hidden_options[name.to_s] = value
17
45
  end
@@ -43,9 +71,15 @@ module Formwandler
43
71
  @default = nil
44
72
  @delocalize = nil
45
73
  @model = nil
74
+ @model_class = nil
46
75
  @source = name
47
76
  @options = nil
48
77
  @array = false
49
78
  end
79
+
80
+ def set_model(model, model_class)
81
+ @model = model
82
+ self.model_class = model_class
83
+ end
50
84
  end
51
85
  end
@@ -14,17 +14,23 @@ module Formwandler
14
14
  ActiveModel::Name.new(self, nil, name.chomp('Form').underscore)
15
15
  end
16
16
 
17
+ def human_attribute_name(attr, options = {})
18
+ field_definition = @field_definitions.fetch(attr) { nil }
19
+ if options[:default].nil? && field_definition && field_definition.model_class
20
+ not_found = '__formwandler_not_found__'
21
+ model_translation = field_definition.model_class.human_attribute_name(field_definition.source, default: not_found)
22
+ options[:default] = model_translation unless model_translation == not_found
23
+ end
24
+ super(attr, options)
25
+ end
26
+
17
27
  def field_definitions
18
28
  @field_definitions ||= {}
19
29
  end
20
30
 
21
31
  def field(name, opts = {}, &block)
22
32
  field_definition = field_definitions[name] ||= FieldDefinition.new(name)
23
- opts.each do |key, value|
24
- field_definition.public_send("#{key}=", value)
25
- rescue NoMethodError
26
- raise ArgumentError, "Invalid option #{key}"
27
- end
33
+ field_definition.configure(opts)
28
34
  field_definition.instance_exec(&block) if block_given?
29
35
 
30
36
  attribute_accessor(name)
@@ -1,3 +1,3 @@
1
1
  module Formwandler
2
- VERSION = '0.7.1'
2
+ VERSION = '0.8.0'
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formwandler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Bilharz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-17 00:00:00.000000000 Z
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
19
+ version: 5.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5'
26
+ version: 5.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: delocalize
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '1.3'
89
+ version: 1.3.6
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '1.3'
96
+ version: 1.3.6
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: byebug
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -142,8 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubyforge_project:
146
- rubygems_version: 2.7.6
145
+ rubygems_version: 3.0.1
147
146
  signing_key:
148
147
  specification_version: 4
149
148
  summary: Form objects for rails