granite-form 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -1
  3. data/.github/workflows/ruby.yml +3 -5
  4. data/Appraisals +1 -1
  5. data/CHANGELOG.md +16 -0
  6. data/README.md +6 -13
  7. data/gemfiles/{rails.5.0.gemfile → rails.7.1.gemfile} +3 -3
  8. data/granite-form.gemspec +4 -4
  9. data/lib/granite/form/model/associations/nested_attributes.rb +2 -2
  10. data/lib/granite/form/model/associations/reflections/embeds_many.rb +1 -1
  11. data/lib/granite/form/model/associations/reflections/embeds_one.rb +11 -1
  12. data/lib/granite/form/model/associations/reflections/references_one.rb +2 -0
  13. data/lib/granite/form/model/associations/reflections/singular.rb +0 -14
  14. data/lib/granite/form/model/attributes/attribute.rb +3 -21
  15. data/lib/granite/form/model/attributes/base.rb +6 -24
  16. data/lib/granite/form/model/attributes/reference_many.rb +1 -1
  17. data/lib/granite/form/model/attributes/reference_one.rb +1 -1
  18. data/lib/granite/form/model/attributes/reflections/attribute.rb +4 -4
  19. data/lib/granite/form/model/attributes/reflections/base/build_type_definition.rb +38 -0
  20. data/lib/granite/form/model/attributes/reflections/base.rb +12 -10
  21. data/lib/granite/form/model/attributes/reflections/collection/build_type_definition.rb +19 -0
  22. data/lib/granite/form/model/attributes/reflections/dictionary/build_type_definition.rb +19 -0
  23. data/lib/granite/form/model/attributes/reflections/dictionary.rb +0 -3
  24. data/lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb +73 -0
  25. data/lib/granite/form/model/attributes/reflections/represents.rb +10 -2
  26. data/lib/granite/form/model/attributes/represents.rb +22 -37
  27. data/lib/granite/form/model/attributes.rb +10 -2
  28. data/lib/granite/form/model/representation.rb +1 -0
  29. data/lib/granite/form/model/validations.rb +6 -0
  30. data/lib/granite/form/model.rb +1 -1
  31. data/lib/granite/form/types/active_support/time_zone.rb +2 -0
  32. data/lib/granite/form/types/array.rb +2 -0
  33. data/lib/granite/form/types/big_decimal.rb +2 -0
  34. data/lib/granite/form/types/boolean.rb +2 -0
  35. data/lib/granite/form/types/collection.rb +11 -0
  36. data/lib/granite/form/types/date.rb +2 -0
  37. data/lib/granite/form/types/date_time.rb +2 -0
  38. data/lib/granite/form/types/dictionary.rb +23 -0
  39. data/lib/granite/form/types/float.rb +2 -0
  40. data/lib/granite/form/types/has_subtype.rb +18 -0
  41. data/lib/granite/form/types/hash_with_action_controller_parameters.rb +2 -0
  42. data/lib/granite/form/types/integer.rb +2 -0
  43. data/lib/granite/form/types/object.rb +28 -0
  44. data/lib/granite/form/types/string.rb +2 -0
  45. data/lib/granite/form/types/time.rb +2 -0
  46. data/lib/granite/form/types/uuid.rb +2 -0
  47. data/lib/granite/form/types.rb +3 -0
  48. data/lib/granite/form/util.rb +55 -0
  49. data/lib/granite/form/version.rb +1 -1
  50. data/lib/granite/form.rb +1 -0
  51. data/spec/granite/form/model/associations/references_many_spec.rb +1 -1
  52. data/spec/granite/form/model/associations/references_one_spec.rb +4 -4
  53. data/spec/granite/form/model/attributes/attribute_spec.rb +0 -29
  54. data/spec/granite/form/model/attributes/base_spec.rb +18 -0
  55. data/spec/granite/form/model/attributes/reflections/attribute_spec.rb +0 -9
  56. data/spec/granite/form/model/attributes/reflections/base/build_type_definition_spec.rb +27 -0
  57. data/spec/granite/form/model/attributes/reflections/base_spec.rb +16 -10
  58. data/spec/granite/form/model/attributes/reflections/collection/build_type_definition_spec.rb +24 -0
  59. data/spec/granite/form/model/attributes/reflections/dictionary/build_type_definition_spec.rb +24 -0
  60. data/spec/granite/form/model/attributes/reflections/dictionary_spec.rb +0 -6
  61. data/spec/granite/form/model/attributes/reflections/represents/build_type_definition_spec.rb +129 -0
  62. data/spec/granite/form/model/attributes/reflections/represents_spec.rb +43 -20
  63. data/spec/granite/form/model/attributes/represents_spec.rb +78 -55
  64. data/spec/granite/form/model/attributes_spec.rb +84 -23
  65. data/spec/granite/form/model/dirty_spec.rb +0 -6
  66. data/spec/granite/form/model/representation_spec.rb +4 -7
  67. data/spec/granite/form/model/validations_spec.rb +28 -1
  68. data/spec/granite/form/types/collection_spec.rb +22 -0
  69. data/spec/granite/form/types/dictionary_spec.rb +32 -0
  70. data/spec/granite/form/types/has_subtype_spec.rb +20 -0
  71. data/spec/granite/form/types/object_spec.rb +50 -4
  72. data/spec/granite/form/util_spec.rb +108 -0
  73. data/spec/support/active_record.rb +3 -0
  74. metadata +40 -31
  75. data/gemfiles/rails.5.1.gemfile +0 -14
  76. data/gemfiles/rails.5.2.gemfile +0 -14
  77. data/lib/granite/form/model/attributes/collection.rb +0 -19
  78. data/lib/granite/form/model/attributes/dictionary.rb +0 -28
  79. data/lib/granite/form/model/attributes/localized.rb +0 -44
  80. data/lib/granite/form/model/attributes/reflections/localized.rb +0 -45
  81. data/lib/granite/form/model/localization.rb +0 -26
  82. data/spec/granite/form/model/attributes/collection_spec.rb +0 -72
  83. data/spec/granite/form/model/attributes/dictionary_spec.rb +0 -100
  84. data/spec/granite/form/model/attributes/localized_spec.rb +0 -103
  85. data/spec/granite/form/model/attributes/reflections/localized_spec.rb +0 -37
@@ -1,28 +0,0 @@
1
- module Granite
2
- module Form
3
- module Model
4
- module Attributes
5
- class Dictionary < Attribute
6
- delegate :keys, to: :reflection
7
-
8
- def read
9
- @value ||= begin
10
- hash = read_before_type_cast
11
- hash = hash.stringify_keys.slice(*keys) if keys.present?
12
-
13
- normalize(Hash[hash.map do |key, value|
14
- [key, enumerize(type_definition.ensure_type(value))]
15
- end].with_indifferent_access).with_indifferent_access
16
- end
17
- end
18
-
19
- def read_before_type_cast
20
- @value_before_type_cast ||= Hash[(@value_cache.presence || {}).map do |key, value|
21
- [key, defaultize(value)]
22
- end].with_indifferent_access
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,44 +0,0 @@
1
- module Granite
2
- module Form
3
- module Model
4
- module Attributes
5
- class Localized < Attribute
6
- def read
7
- @value ||= Hash[read_before_type_cast.map do |locale, value|
8
- [locale.to_s, normalize(enumerize(type_definition.ensure_type(value)))]
9
- end]
10
- end
11
-
12
- def read_before_type_cast
13
- @value_before_type_cast ||= Hash[(@value_cache.presence || {}).map do |locale, value|
14
- [locale.to_s, defaultize(value)]
15
- end]
16
- end
17
-
18
- def write_locale(value, locale)
19
- pollute do
20
- write(read.merge(locale.to_s => value))
21
- end
22
- end
23
-
24
- def read_locale(locale)
25
- read[owner.class.fallbacks(locale).detect do |fallback|
26
- read[fallback.to_s]
27
- end.to_s]
28
- end
29
-
30
- def read_locale_before_type_cast(locale)
31
- read_before_type_cast[owner.class.fallbacks(locale).detect do |fallback|
32
- read_before_type_cast[fallback.to_s]
33
- end.to_s]
34
- end
35
-
36
- def locale_query(locale)
37
- value = read_locale(locale)
38
- !(value.respond_to?(:zero?) ? value.zero? : value.blank?)
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
@@ -1,45 +0,0 @@
1
- module Granite
2
- module Form
3
- module Model
4
- module Attributes
5
- module Reflections
6
- class Localized < Attribute
7
- def self.build(target, generated_methods, name, *args, &block)
8
- attribute = super(target, generated_methods, name, *args, &block)
9
- generate_methods name, generated_methods
10
- attribute
11
- end
12
-
13
- def self.generate_methods(name, target)
14
- target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
- def #{name}_translations
16
- attribute('#{name}').read
17
- end
18
-
19
- def #{name}_translations= value
20
- attribute('#{name}').write(value)
21
- end
22
-
23
- def #{name}
24
- attribute('#{name}').read_locale(self.class.locale)
25
- end
26
-
27
- def #{name}= value
28
- attribute('#{name}').write_locale(value, self.class.locale)
29
- end
30
-
31
- def #{name}?
32
- attribute('#{name}').locale_query(self.class.locale)
33
- end
34
-
35
- def #{name}_before_type_cast
36
- attribute('#{name}').read_locale_before_type_cast(self.class.locale)
37
- end
38
- RUBY
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,26 +0,0 @@
1
- require 'granite/form/model/attributes/reflections/localized'
2
- require 'granite/form/model/attributes/localized'
3
-
4
- module Granite
5
- module Form
6
- module Model
7
- module Localization
8
- extend ActiveSupport::Concern
9
-
10
- module ClassMethods
11
- def localized(*args, &block)
12
- add_attribute(Granite::Form::Model::Attributes::Reflections::Localized, *args, &block)
13
- end
14
-
15
- def fallbacks(locale)
16
- ::I18n.respond_to?(:fallbacks) ? ::I18n.fallbacks[locale] : [locale]
17
- end
18
-
19
- def locale
20
- I18n.locale
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,72 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Granite::Form::Model::Attributes::Collection do
4
- before { stub_model(:dummy) }
5
-
6
- def attribute(*args)
7
- options = args.extract_options!
8
- Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Collection, :field, options)
9
- Dummy.new.attribute(:field)
10
- end
11
-
12
- describe '#read' do
13
- let(:field) { attribute(type: String, normalizer: ->(v) { v.uniq }, default: :world, enum: %w[hello 42]) }
14
-
15
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
16
- specify { expect(field.tap { |r| r.write([nil]) }.read).to eq([nil]) }
17
- specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
18
- specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
19
- specify { expect(field.tap { |r| r.write([43]) }.read).to eq([nil]) }
20
- specify { expect(field.tap { |r| r.write([43, 44]) }.read).to eq([nil]) }
21
- specify { expect(field.tap { |r| r.write(['']) }.read).to eq([nil]) }
22
- specify { expect(field.tap { |r| r.write(['hello', 42]) }.read).to eq(%w[hello 42]) }
23
- specify { expect(field.tap { |r| r.write(['hello', false]) }.read).to eq(['hello', nil]) }
24
-
25
- context do
26
- let(:field) { attribute(type: String, normalizer: ->(v) { v.uniq }, default: :world) }
27
-
28
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
29
- specify { expect(field.tap { |r| r.write([nil, nil]) }.read).to eq(['world']) }
30
- specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
31
- specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
32
- specify { expect(field.tap { |r| r.write(['']) }.read).to eq(['']) }
33
- end
34
- end
35
-
36
- describe '#read_before_type_cast' do
37
- let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
38
-
39
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
40
- specify { expect(field.tap { |r| r.write([nil]) }.read_before_type_cast).to eq([:world]) }
41
- specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
42
- specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
43
- specify { expect(field.tap { |r| r.write([43]) }.read_before_type_cast).to eq([43]) }
44
- specify { expect(field.tap { |r| r.write([43, 44]) }.read_before_type_cast).to eq([43, 44]) }
45
- specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
46
- specify { expect(field.tap { |r| r.write(['hello', 42]) }.read_before_type_cast).to eq(['hello', 42]) }
47
- specify { expect(field.tap { |r| r.write(['hello', false]) }.read_before_type_cast).to eq(['hello', false]) }
48
-
49
- context do
50
- let(:field) { attribute(type: String, default: :world) }
51
-
52
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
53
- specify { expect(field.tap { |r| r.write([nil, nil]) }.read_before_type_cast).to eq(%i[world world]) }
54
- specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
55
- specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
56
- specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
57
- end
58
- end
59
-
60
- context 'integration' do
61
- before do
62
- stub_model(:post) do
63
- collection :tags, String
64
- end
65
- end
66
-
67
- specify { expect(Post.new(tags: ['hello', 42]).tags).to eq(%w[hello 42]) }
68
- specify { expect(Post.new(tags: ['hello', 42]).tags_before_type_cast).to eq(['hello', 42]) }
69
- specify { expect(Post.new.tags?).to eq(false) }
70
- specify { expect(Post.new(tags: ['hello', 42]).tags?).to eq(true) }
71
- end
72
- end
@@ -1,100 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Granite::Form::Model::Attributes::Dictionary do
4
- before { stub_model(:dummy) }
5
-
6
- def attribute(*args)
7
- options = args.extract_options!
8
- Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Dictionary, :field, options)
9
- Dummy.new.attribute(:field)
10
- end
11
-
12
- describe '#read' do
13
- let(:field) do
14
- attribute(type: String,
15
- normalizer: ->(val) { val.delete_if { |_, v| v.nil? } },
16
- default: :world, enum: %w[hello 42])
17
- end
18
-
19
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
20
- specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
21
- specify { expect(field.tap { |r| r.write(a: nil) }.read).to eq({}) }
22
- specify { expect(field.tap { |r| r.write(a: '') }.read).to eq({}) }
23
- specify { expect(field.tap { |r| r.write(a: 1) }.read).to eq({}) }
24
- specify { expect(field.tap { |r| r.write(a: 42) }.read).to eq('a' => '42') }
25
- specify { expect(field.tap { |r| r.write(a: 'hello', b: '42') }.read).to eq('a' => 'hello', 'b' => '42') }
26
- specify { expect(field.tap { |r| r.write(a: 'hello', b: '1') }.read).to eq('a' => 'hello') }
27
- specify { expect(field.tap { |r| r.write(a: 'hello', x: '42') }.read).to eq('a' => 'hello', 'x' => '42') }
28
-
29
- context do
30
- let(:field) do
31
- attribute(type: String,
32
- normalizer: ->(val) { val.delete_if { |_, v| v.nil? } },
33
- default: :world)
34
- end
35
-
36
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
37
- specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
38
- specify { expect(field.tap { |r| r.write(a: 1) }.read).to eq('a' => '1') }
39
- specify { expect(field.tap { |r| r.write(a: nil, b: nil) }.read).to eq('a' => 'world', 'b' => 'world') }
40
- specify { expect(field.tap { |r| r.write(a: '') }.read).to eq('a' => '') }
41
- end
42
-
43
- context 'with :keys' do
44
- let(:field) { attribute(type: String, keys: ['a', :b]) }
45
-
46
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
47
- specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
48
- specify { expect(field.tap { |r| r.write(a: 1, 'b' => 2, c: 3, 'd' => 4) }.read).to eq('a' => '1', 'b' => '2') }
49
- specify { expect(field.tap { |r| r.write(a: 1, c: 3, 'd' => 4) }.read).to eq('a' => '1') }
50
- end
51
- end
52
-
53
- describe '#read_before_type_cast' do
54
- let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
55
-
56
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
57
- specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
58
- specify { expect(field.tap { |r| r.write(a: 1) }.read_before_type_cast).to eq('a' => 1) }
59
- specify { expect(field.tap { |r| r.write(a: nil) }.read_before_type_cast).to eq('a' => :world) }
60
- specify { expect(field.tap { |r| r.write(a: '') }.read_before_type_cast).to eq('a' => '') }
61
- specify { expect(field.tap { |r| r.write(a: 42) }.read_before_type_cast).to eq('a' => 42) }
62
- specify { expect(field.tap { |r| r.write(a: 'hello', b: '42') }.read_before_type_cast).to eq('a' => 'hello', 'b' => '42') }
63
- specify { expect(field.tap { |r| r.write(a: 'hello', b: '1') }.read_before_type_cast).to eq('a' => 'hello', 'b' => '1') }
64
- specify { expect(field.tap { |r| r.write(a: 'hello', x: '42') }.read_before_type_cast).to eq('a' => 'hello', 'x' => '42') }
65
-
66
- context do
67
- let(:field) { attribute(type: String, default: :world) }
68
-
69
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
70
- specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
71
- specify { expect(field.tap { |r| r.write(a: 1) }.read_before_type_cast).to eq('a' => 1) }
72
- specify { expect(field.tap { |r| r.write(a: nil, b: nil) }.read_before_type_cast).to eq('a' => :world, 'b' => :world) }
73
- specify { expect(field.tap { |r| r.write(a: '') }.read_before_type_cast).to eq('a' => '') }
74
- end
75
-
76
- context 'with :keys' do
77
- let(:field) { attribute(type: String, keys: ['a', :b]) }
78
-
79
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
80
- specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
81
- specify do
82
- expect(field.tap { |r| r.write(a: 1, 'b' => 2, c: 3, 'd' => 4) }.read_before_type_cast)
83
- .to eq('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4)
84
- end
85
- end
86
- end
87
-
88
- context 'integration' do
89
- before do
90
- stub_model(:post) do
91
- dictionary :options, type: String
92
- end
93
- end
94
-
95
- specify { expect(Post.new(options: {a: 'hello', b: 42}).options).to eq('a' => 'hello', 'b' => '42') }
96
- specify { expect(Post.new(options: {a: 'hello', b: 42}).options_before_type_cast).to eq('a' => 'hello', 'b' => 42) }
97
- specify { expect(Post.new.options?).to eq(false) }
98
- specify { expect(Post.new(options: {a: 'hello', b: 42}).options?).to eq(true) }
99
- end
100
- end
@@ -1,103 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Granite::Form::Model::Attributes::Localized do
4
- before { stub_model(:dummy) }
5
-
6
- def attribute(*args)
7
- options = args.extract_options!
8
- Dummy.add_attribute(Granite::Form::Model::Attributes::Reflections::Localized, :field, options)
9
- Dummy.new.attribute(:field)
10
- end
11
-
12
- describe '#read' do
13
- let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
14
-
15
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
16
- specify { expect(field.tap { |r| r.write(en: 'hello') }.read).to eq('en' => 'hello') }
17
- specify { expect(field.tap { |r| r.write(en: 42) }.read).to eq('en' => '42') }
18
- specify { expect(field.tap { |r| r.write(en: 43) }.read).to eq('en' => nil) }
19
- specify { expect(field.tap { |r| r.write(en: '') }.read).to eq('en' => nil) }
20
- specify { expect(field.tap { |r| r.write(en: nil) }.read).to eq('en' => nil) }
21
- specify { expect(field.tap { |r| r.write(en: 'hello', ru: 42) }.read).to eq('en' => 'hello', 'ru' => '42') }
22
-
23
- context do
24
- let(:field) { attribute(type: String, default: :world) }
25
-
26
- specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
27
- specify { expect(field.tap { |r| r.write(en: 'hello') }.read).to eq('en' => 'hello') }
28
- specify { expect(field.tap { |r| r.write(en: 42) }.read).to eq('en' => '42') }
29
- specify { expect(field.tap { |r| r.write(en: '') }.read).to eq('en' => '') }
30
- specify { expect(field.tap { |r| r.write(en: nil) }.read).to eq('en' => 'world') }
31
- end
32
- end
33
-
34
- describe '#read_before_type_cast' do
35
- let(:field) { attribute(type: String, default: :world, enum: %w[hello 42]) }
36
-
37
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
38
- specify { expect(field.tap { |r| r.write(en: 'hello') }.read_before_type_cast).to eq('en' => 'hello') }
39
- specify { expect(field.tap { |r| r.write(en: 42) }.read_before_type_cast).to eq('en' => 42) }
40
- specify { expect(field.tap { |r| r.write(en: 43) }.read_before_type_cast).to eq('en' => 43) }
41
- specify { expect(field.tap { |r| r.write(en: '') }.read_before_type_cast).to eq('en' => '') }
42
- specify { expect(field.tap { |r| r.write(en: nil) }.read_before_type_cast).to eq('en' => :world) }
43
- specify { expect(field.tap { |r| r.write(en: 'hello', ru: 42) }.read_before_type_cast).to eq('en' => 'hello', 'ru' => 42) }
44
-
45
- context do
46
- let(:field) { attribute(type: String, default: :world) }
47
-
48
- specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
49
- specify { expect(field.tap { |r| r.write(en: 'hello') }.read_before_type_cast).to eq('en' => 'hello') }
50
- specify { expect(field.tap { |r| r.write(en: 42) }.read_before_type_cast).to eq('en' => 42) }
51
- specify { expect(field.tap { |r| r.write(en: '') }.read_before_type_cast).to eq('en' => '') }
52
- specify { expect(field.tap { |r| r.write(en: nil) }.read_before_type_cast).to eq('en' => :world) }
53
- end
54
- end
55
-
56
- context 'integration' do
57
- let(:klass) do
58
- Class.new do
59
- include Granite::Form::Model
60
- include Granite::Form::Model::Localization
61
-
62
- localized :name, type: String
63
- end
64
- end
65
- let(:translations) { {en: 'Hello', ru: 'Привет'} }
66
- before { I18n.locale = :en }
67
-
68
- describe '#name_translations' do
69
- subject { klass.new name_translations: translations }
70
- its(:name_translations) { should == translations.stringify_keys }
71
- its(:name) { should == translations[:en] }
72
- end
73
-
74
- describe '#name' do
75
- subject { klass.new name: 'Hello' }
76
- its(:name_translations) { should == {'en' => 'Hello'} }
77
- its(:name) { should == 'Hello' }
78
- end
79
-
80
- describe '#name_before_type_cast' do
81
- let(:object) { Object.new }
82
- subject { klass.new name: object }
83
- its(:name_before_type_cast) { should == object }
84
- end
85
-
86
- context 'fallbacks' do
87
- subject { klass.new name_translations: {ru: 'Привет'} }
88
- context do
89
- its(:name) { should be_nil }
90
- end
91
-
92
- context do
93
- before do
94
- require 'i18n/backend/fallbacks'
95
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
96
- I18n.fallbacks[:en] = [:ru]
97
- end
98
- after { I18n.fallbacks = false }
99
- its(:name) { should == 'Привет' }
100
- end
101
- end
102
- end
103
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Granite::Form::Model::Attributes::Reflections::Localized do
4
- def reflection(options = {})
5
- described_class.new(:field, options)
6
- end
7
-
8
- describe '.build' do
9
- before { stub_class(:target) }
10
-
11
- specify do
12
- described_class.build(Class.new, Target, :field)
13
-
14
- expect(Target).to be_method_defined(:field_translations)
15
- expect(Target).to be_method_defined(:field_translations=)
16
- expect(Target).to be_method_defined(:field)
17
- expect(Target).to be_method_defined(:field=)
18
- expect(Target).to be_method_defined(:field?)
19
- expect(Target).to be_method_defined(:field_before_type_cast)
20
- end
21
- end
22
-
23
- describe '#generate_methods' do
24
- before { stub_class(:target) }
25
-
26
- specify do
27
- described_class.generate_methods(:field_alias, Target)
28
-
29
- expect(Target).to be_method_defined(:field_alias_translations)
30
- expect(Target).to be_method_defined(:field_alias_translations=)
31
- expect(Target).to be_method_defined(:field_alias)
32
- expect(Target).to be_method_defined(:field_alias=)
33
- expect(Target).to be_method_defined(:field_alias?)
34
- expect(Target).to be_method_defined(:field_alias_before_type_cast)
35
- end
36
- end
37
- end