custom_fielder 0.5.8 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9439a06e0eb1e9a999f50de592dbf8450e8337de
4
- data.tar.gz: e4d3f9b9c1a4ff3ae81f35af6ae85bdf68d2e2da
3
+ metadata.gz: ba1a35b8ba0a749348e09d583d71ca7289338b1d
4
+ data.tar.gz: 0c82c5566b04597e87ec82659a4fa46fe36cc173
5
5
  SHA512:
6
- metadata.gz: 2e4a9af79b87f83d457ce584d10231ea2dd7d15a2944be38dee663235c75d80d4666a83ed310a506f849252da35d6b29d620566af2019b66e5e42c8085fd3d9c
7
- data.tar.gz: 7abc545e853aeafb53bed45ab0d1f45913835defa27fcb3cf85cabda595377a84140c3c2a82f745e170c7291355190780af996f1ac186941defbdaaa890c6f3f
6
+ metadata.gz: fb0fdd4f0c2cb97a7155a5753a939d63efe10b4d777a5acb5311caad7eda66630d3873ee1a9b95e239bd316d168b8c099197922ae407d1628aff9b449a9d6880
7
+ data.tar.gz: 1ab60451a738ac239bd040b7b9f91752cc46149cbb92637d4a374524d46ed6af5997dd15bfbf097a63a5618b351e1d4ae0ddf607a96ed5bb39fb3349aadbd82c
@@ -0,0 +1,11 @@
1
+ module CustomFielder
2
+ class Field < ActiveRecord::Base
3
+ has_many :values
4
+
5
+ validates :name, presence: true
6
+ validates :field_type, presence: true,
7
+ inclusion: { in: %w[Array Integer Float Date DateTime Boolean String] }
8
+ validates :fieldable_type, presence: true
9
+ validates :allow_blank, inclusion: { in: [true, false] }
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module CustomFielder
2
- class CustomField < ActiveRecord::Base
2
+ class Value < ActiveRecord::Base
3
3
  include TypeCheckingHelper
4
4
  include DeserializationHelper
5
5
 
@@ -8,18 +8,16 @@ module CustomFielder
8
8
  validate :check_type
9
9
  validate :check_allow_blank
10
10
  validate :check_allowed_values
11
+ validate :check_fieldable_type
11
12
 
12
- belongs_to :custom_fieldable, polymorphic: true
13
- belongs_to :field_attributes, dependent: :destroy, class_name: 'FieldAttributes'
13
+ belongs_to :field, dependent: :destroy
14
+ belongs_to :fieldable, polymorphic: true
14
15
 
15
16
  scope :active, -> { where(active: true) }
16
17
  scope :inactive, -> { where(active: false) }
17
18
 
18
- scope :fields_with_class, -> (klass) { where(custom_fieldable_type: klass) }
19
- scope :fields_with_attributes, -> (field_attrs) { where(field_attributes: field_attrs) }
20
- scope :fields_with_class_and_attributes, -> (klass, field_attrs) do
21
- where(custom_fieldable_type: klass, field_attributes: field_attrs)
22
- end
19
+ scope :values_with_field, -> (field) { where(field: field) }
20
+ scope :values_with_class, -> (klass) { where(fieldable_type: klass) }
23
21
 
24
22
  ##
25
23
  # Because everything is stored as string with ActiveRecord
@@ -35,25 +33,25 @@ module CustomFielder
35
33
  ##
36
34
  # Activates all CustomFields that belong to passed field attributes
37
35
  #
38
- # @param field_attrs [CustomField::FieldAttributes]
36
+ # @param field_attrs [CustomFielder::Field]
39
37
  #
40
38
  # @return [Nil]
41
39
  #
42
- def activate_field_for_all(field_attrs)
43
- fields = all.where(field_attributes: field_attrs)
44
- fields.each { |field| field.active = true; field.save }
40
+ def activate_field_for_all(field)
41
+ values = all.where(field: field)
42
+ values.each { |value| value.active = true; value.save }
45
43
  end
46
44
 
47
45
  ##
48
46
  # Deactivates all CustomFields that belong to passed field attributes
49
47
  #
50
- # @param field_attrs [CustomField::FieldAttributes]
48
+ # @param field_attrs [CustomFielder::Field]
51
49
  #
52
50
  # @return [Nil]
53
51
  #
54
- def deactivate_field_for_all(field_attrs)
55
- fields = all.where(field_attributes: field_attrs)
56
- fields.each { |field| field.active = false; field.save }
52
+ def deactivate_field_for_all(field)
53
+ values = all.where(field: field)
54
+ values.each { |value| value.active = false; value.save }
57
55
  end
58
56
  end
59
57
 
@@ -65,7 +63,16 @@ module CustomFielder
65
63
  # @return [String]
66
64
  #
67
65
  def type
68
- @type ||= field_attributes.field_type
66
+ @type ||= field.field_type
67
+ end
68
+
69
+ ##
70
+ # Gets the fieldable type from the field
71
+ #
72
+ # @return [String]
73
+ #
74
+ def field_fieldable_type
75
+ @field_fieldable_type ||= field.fieldable_type
69
76
  end
70
77
 
71
78
  ##
@@ -74,7 +81,7 @@ module CustomFielder
74
81
  # @return [Boolean]
75
82
  #
76
83
  def allow_blank
77
- @allow_blank ||= field_attributes.allow_blank
84
+ @allow_blank ||= field.allow_blank
78
85
  end
79
86
 
80
87
  ##
@@ -84,8 +91,8 @@ module CustomFielder
84
91
  #
85
92
  def allowed_values
86
93
  @allowed_values ||= begin
87
- unless field_attributes.allowed_values.nil?
88
- deserialize('Array', field_attributes.allowed_values)
94
+ unless field.allowed_values.nil?
95
+ deserialize('Array', field.allowed_values)
89
96
  else
90
97
  nil
91
98
  end
@@ -98,7 +105,7 @@ module CustomFielder
98
105
  # @return [String]
99
106
  #
100
107
  def default
101
- @default ||= field_attributes.default
108
+ @default ||= field.default
102
109
  end
103
110
 
104
111
  ##
@@ -142,6 +149,13 @@ module CustomFielder
142
149
  value_not_allowed_error unless allowed_values.include?(value)
143
150
  end
144
151
 
152
+ ##
153
+ # @return [Nil] Adds error if invalid
154
+ #
155
+ def check_fieldable_type
156
+ value_fieldable_type_error unless field_fieldable_type == fieldable_type
157
+ end
158
+
145
159
  ##
146
160
  # If a value is passed that does not match field type adds
147
161
  # a mismatch error
@@ -149,7 +163,7 @@ module CustomFielder
149
163
  # @return [Nil]
150
164
  #
151
165
  def value_mismatch_error
152
- errors.add(:value, 'value passed does not match field type')
166
+ errors.add(:value, 'Value passed does not match field type')
153
167
  end
154
168
 
155
169
  ##
@@ -159,7 +173,7 @@ module CustomFielder
159
173
  # @return [Nil]
160
174
  #
161
175
  def value_blank_error
162
- errors.add(:value, 'blank value passed when allow blank is false')
176
+ errors.add(:value, 'Blank value not allowed')
163
177
  end
164
178
 
165
179
  ##
@@ -169,7 +183,17 @@ module CustomFielder
169
183
  # @return [Nil]
170
184
  #
171
185
  def value_not_allowed_error
172
- errors.add(:value, 'value passed not in allowed values')
186
+ errors.add(:value, 'Value passed not in options')
187
+ end
188
+
189
+ ##
190
+ # If a value is trying to be assigned to a model that is different
191
+ # than the one the field was made for adds value fieldable type error
192
+ #
193
+ # @return [Nil]
194
+ #
195
+ def value_fieldable_type_error
196
+ errors.add(:value, "Can't assign value to a #{fieldable_type} when field is for #{field_fieldable_type}")
173
197
  end
174
198
 
175
199
  ##
@@ -0,0 +1,13 @@
1
+ class CreateCustomFielderValues < ActiveRecord::Migration
2
+ def change
3
+ create_table :custom_fielder_values do |t|
4
+ t.references :fieldable, polymorphic: true, null: false, index: { name: 'index_values_on_fieldable_type_and_fieldable_id' }
5
+ t.integer :field_id, null: false
6
+ t.boolean :active, null: false, default: true
7
+ t.string :value
8
+ t.text :note
9
+
10
+ t.timestamps null: false
11
+ end
12
+ end
13
+ end
@@ -1,8 +1,9 @@
1
- class CreateCustomFielderFieldAttributes < ActiveRecord::Migration
1
+ class CreateCustomFielderFields < ActiveRecord::Migration
2
2
  def change
3
- create_table :custom_fielder_field_attributes do |t|
3
+ create_table :custom_fielder_fields do |t|
4
4
  t.string :name, null: false
5
5
  t.string :field_type, null: false
6
+ t.string :fieldable_type, null: false
6
7
  t.string :default
7
8
  t.boolean :allow_blank, null: false, default: false
8
9
  t.string :allowed_values
@@ -10,5 +11,7 @@ class CreateCustomFielderFieldAttributes < ActiveRecord::Migration
10
11
 
11
12
  t.timestamps null: false
12
13
  end
14
+
15
+ add_index :custom_fielder_fields, [:name, :fieldable_type], unique: true
13
16
  end
14
17
  end
@@ -3,21 +3,22 @@ module CustomFielder
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- has_many :custom_fielder_custom_fields,
7
- as: :custom_fieldable,
6
+ has_many :custom_fielder_fields,
7
+ through: :custom_fielder_values,
8
+ class_name: 'CustomFielder::Field',
9
+ source: :field
10
+
11
+ has_many :custom_fielder_values,
12
+ as: :fieldable,
8
13
  dependent: :destroy,
9
- class_name: 'CustomFielder::CustomField'
14
+ class_name: 'CustomFielder::Value'
15
+
10
16
 
11
- has_many :custom_fielder_field_attributes,
12
- through: :custom_fielder_custom_fields,
13
- class_name: 'CustomFielder::FieldAttributes',
14
- source: :field_attributes
17
+ accepts_nested_attributes_for :custom_fielder_values, allow_destroy: true
18
+ accepts_nested_attributes_for :custom_fielder_fields, allow_destroy: false
15
19
 
16
- accepts_nested_attributes_for :custom_fielder_custom_fields, allow_destroy: true
17
- accepts_nested_attributes_for :custom_fielder_field_attributes, allow_destroy: false
18
-
19
- alias_method :custom_fields, :custom_fielder_custom_fields
20
- alias_method :custom_field_attributes, :custom_fielder_field_attributes
20
+ alias_method :custom_fields, :custom_fielder_fields
21
+ alias_method :custom_field_values, :custom_fielder_values
21
22
 
22
23
  ##
23
24
  # Returns a hash where the keys are the field names
@@ -26,9 +27,9 @@ module CustomFielder
26
27
  # @return [Hash]
27
28
  #
28
29
  def get_custom_fields_with_values
29
- custom_fields.inject(Hash.new) do |h, cf|
30
- field_name = cf.field_attributes.name
31
- h[field_name] = cf.value; h
30
+ custom_field_values.inject(Hash.new) do |h, field_value|
31
+ field_name = field_value.field.name
32
+ h[field_name] = field_value.value; h
32
33
  end
33
34
  end
34
35
  end
@@ -1,3 +1,3 @@
1
1
  module CustomFielder
2
- VERSION = "0.5.8"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: custom_fielder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Hurst
@@ -67,12 +67,12 @@ files:
67
67
  - app/helpers/custom_fielder/application_helper.rb
68
68
  - app/helpers/custom_fielder/deserialization_helper.rb
69
69
  - app/helpers/custom_fielder/type_checking_helper.rb
70
- - app/models/custom_fielder/custom_field.rb
71
- - app/models/custom_fielder/field_attributes.rb
70
+ - app/models/custom_fielder/field.rb
71
+ - app/models/custom_fielder/value.rb
72
72
  - app/views/layouts/custom_fielder/application.html.erb
73
73
  - config/routes.rb
74
- - db/migrate/20160525133243_create_custom_fielder_custom_fields.rb
75
- - db/migrate/20160525133922_create_custom_fielder_field_attributes.rb
74
+ - db/migrate/20160525133243_create_custom_fielder_values.rb
75
+ - db/migrate/20160525133922_create_custom_fielder_fields.rb
76
76
  - lib/custom_fielder.rb
77
77
  - lib/custom_fielder/custom_fieldable.rb
78
78
  - lib/custom_fielder/engine.rb
@@ -1,18 +0,0 @@
1
- module CustomFielder
2
- class FieldAttributes < ActiveRecord::Base
3
- has_many :custom_fields
4
-
5
- before_save :serialize_allowed_values
6
-
7
- validates :name, presence: true
8
- validates :field_type, presence: true,
9
- inclusion: { in: %w[Array Integer Float Date DateTime Boolean String] }
10
- validates :allow_blank, inclusion: { in: [true, false] }
11
-
12
- private
13
-
14
- def serialize_allowed_values
15
- allowed_values = allowed_values.to_s
16
- end
17
- end
18
- end
@@ -1,13 +0,0 @@
1
- class CreateCustomFielderCustomFields < ActiveRecord::Migration
2
- def change
3
- create_table :custom_fielder_custom_fields do |t|
4
- t.references :custom_fieldable, polymorphic: true, index: { name: 'index_custom_fields_on_fieldable_type_and_fieldable_id' }, null: false
5
- t.integer :field_attributes_id, null: false
6
- t.boolean :active, null: false, default: true
7
- t.string :value
8
- t.text :note
9
-
10
- t.timestamps null: false
11
- end
12
- end
13
- end