property_sets 3.8.0 → 3.12.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: ddab71bb5879d419307ca07fda5be61c6cacfb3dd3e5881457a9eaa88bf94888
4
- data.tar.gz: 1b5b98ff2a14ef8797ae7a1b45ee8f6e6c7b2ccfd0dc65bbcfb27cde59ea28e3
3
+ metadata.gz: 89dc53f27e14856ad59c07ed529ab9970e56340f6b6a57dceb99e663c0518ea9
4
+ data.tar.gz: 193dd9180561e7fb71ede29694e9fee63d5ef6520c16dcf824831828f2b1fb77
5
5
  SHA512:
6
- metadata.gz: ae6d4df148fdb6d1743abbd9986a33a8392064d45aa6ce36d871c64b16383c1d8279b3c29c1b627fef429d36cd0cabecba19b56b61b20ab3b6647716190faf40
7
- data.tar.gz: 7a4834b04e57c715883aa6bc27080820b8d4f74101cf7385df506800ead33f3944f6c9106fde57ded468107ced9cfa40ba33ea8151665076e689a13ef23e3e5d
6
+ metadata.gz: 34512dfab029b1a2c1b5112bf95e7a4fe3d3dbba4eea1d24e5d8e967ad58fe9ef4a652692f83896cd8bc3faeab0313ce7c3111532b3ffd50fc6f44f189bb196e
7
+ data.tar.gz: 4cf9c6cc2b2dbb9cc513eda7c8b32f0d884f39ae3828f68d312c44f82834ecda29e030cf1860103ff8694785240e8485dfb2368df470a5f4ad43cf26a087865f
@@ -1,4 +1,4 @@
1
- require 'action_view'
1
+ require "action_view"
2
2
 
3
3
  module ActionView
4
4
  module Helpers
@@ -8,9 +8,9 @@ module ActionView
8
8
 
9
9
  def initialize(property_set, template, object_name, object)
10
10
  @property_set = property_set
11
- @template = template
12
- @object_name = object_name
13
- @object = object
11
+ @template = template
12
+ @object_name = object_name
13
+ @object = object
14
14
  end
15
15
 
16
16
  def check_box(property, options = {}, checked_value = "1", unchecked_value = "0")
@@ -23,7 +23,7 @@ module ActionView
23
23
  def radio_button(property, checked_value = "1", options = {})
24
24
  options[:id] ||= "#{object_name}_#{property_set}_#{property}_#{checked_value}"
25
25
  options = prepare_options(property, options) do |properties|
26
- properties.send("#{property}") == checked_value
26
+ properties.send(property.to_s) == checked_value
27
27
  end
28
28
  template.radio_button(object_name, property, checked_value, options)
29
29
  end
@@ -34,7 +34,7 @@ module ActionView
34
34
 
35
35
  def hidden_field(property, options = {})
36
36
  options = prepare_id_name(property, options)
37
- unless options.keys.include?(:value)
37
+ unless options.key?(:value)
38
38
  options[:value] = cast_boolean(options[:object].send(property_set).send(property))
39
39
  end
40
40
  template.hidden_field(object_name, property, options)
@@ -43,7 +43,7 @@ module ActionView
43
43
  def select(property, choices, options = {}, html_options = {})
44
44
  options = prepare_id_name(property, options)
45
45
  current_value = options[:object].send(property_set).send(property)
46
- template.select("#{object_name}[#{property_set}]", property, choices, { :selected => current_value }, html_options )
46
+ template.select("#{object_name}[#{property_set}]", property, choices, {selected: current_value}, html_options)
47
47
  end
48
48
 
49
49
  private
@@ -52,9 +52,9 @@ module ActionView
52
52
  throw "Invalid options type #{options.inspect}" unless options.is_a?(Hash)
53
53
 
54
54
  options.clone.tap do |prepared_options|
55
- prepared_options[:object] = object || fetch_target_object
56
- prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}"
57
- prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]"
55
+ prepared_options[:object] = object || fetch_target_object
56
+ prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}"
57
+ prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]"
58
58
  end
59
59
  end
60
60
 
@@ -75,18 +75,16 @@ module ActionView
75
75
 
76
76
  def cast_boolean(value)
77
77
  case value
78
- when TrueClass then '1'
79
- when FalseClass then '0'
78
+ when TrueClass then "1"
79
+ when FalseClass then "0"
80
80
  else value
81
81
  end
82
82
  end
83
-
84
83
  end
85
84
 
86
85
  def property_set(identifier)
87
86
  PropertySetFormBuilderProxy.new(identifier, @template, object_name, object)
88
87
  end
89
-
90
88
  end
91
89
  end
92
90
  end
@@ -1,29 +1,27 @@
1
- require 'active_record'
2
- require 'property_sets/casting'
3
- require 'set'
1
+ require "active_record"
2
+ require "property_sets/casting"
4
3
 
5
4
  module PropertySets
6
5
  module ActiveRecordExtension
7
6
  module ClassMethods
8
-
9
- RAILS6 = ActiveRecord::VERSION::MAJOR >= 6
7
+ attr_accessor :property_sets_connection_class
10
8
 
11
9
  def property_set(association, options = {}, &block)
12
10
  unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
13
- self.send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods)
11
+ send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods)
14
12
  cattr_accessor :property_set_index
15
13
  self.property_set_index = Set.new
16
14
  end
17
15
 
18
- raise "Invalid association name, letters only" unless association.to_s =~ /[a-z]+/
16
+ raise "Invalid association name, letters only" unless /[a-z]+/.match?(association.to_s)
19
17
  exists = property_set_index.include?(association)
20
18
 
21
- self.property_set_index << association
19
+ property_set_index << association
22
20
 
23
21
  # eg AccountSetting - this IS idempotent
24
22
  property_class = PropertySets.ensure_property_set_class(
25
23
  association,
26
- options.delete(:owner_class_name) || self.name
24
+ options.delete(:owner_class_name) || name
27
25
  )
28
26
 
29
27
  # eg property :is_awesome
@@ -33,48 +31,42 @@ module PropertySets
33
31
  property_class.table_name = tb_name if tb_name
34
32
 
35
33
  hash_opts = {
36
- :class_name => property_class.name,
37
- :autosave => true,
38
- :dependent => :destroy,
39
- :inverse_of => self.name.underscore.to_sym,
34
+ class_name: property_class.name,
35
+ autosave: true,
36
+ dependent: :destroy,
37
+ inverse_of: name.demodulize.underscore.to_sym
40
38
  }.merge(options)
41
39
 
42
40
  # TODO: should check options are compatible? warn? raise?
43
- reflection = self.reflections[association.to_s] # => ActiveRecord::Reflection::HasManyReflection
41
+ reflection = reflections[association.to_s] # => ActiveRecord::Reflection::HasManyReflection
44
42
  reflection.options.merge! options if reflection && !options.empty?
45
43
 
46
- unless exists then # makes has_many idempotent...
44
+ unless exists # makes has_many idempotent...
47
45
  has_many association, **hash_opts do
48
46
  # keep this damn block! -- creates association_module below
49
47
  end
50
48
  end
51
49
 
52
- # eg 5: AccountSettingsAssociationExtension
53
- # eg 6: Account::SettingsAssociationExtension
54
-
55
50
  # stolen/adapted from AR's collection_association.rb #define_extensions
56
51
 
57
52
  module_name = "#{association.to_s.camelize}AssociationExtension"
58
- module_name = name.demodulize + module_name unless RAILS6
59
-
60
- target = RAILS6 ? self : self.parent
61
- association_module = target.const_get module_name
53
+ association_module = const_get module_name
62
54
 
63
55
  association_module.module_eval do
64
56
  include PropertySets::ActiveRecordExtension::AssociationExtensions
65
57
 
66
58
  property_class.keys.each do |key|
67
- raise "Invalid property key #{key}" if self.respond_to?(key)
59
+ raise "Invalid property key #{key}" if respond_to?(key)
68
60
 
69
61
  # Reports the coerced truth value of the property
70
62
  define_method "#{key}?" do
71
- type = property_class.type(key)
63
+ type = property_class.type(key)
72
64
  value = lookup_value(type, key)
73
- ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
65
+ !["false", "0", "", "off", "n"].member?(value.to_s.downcase)
74
66
  end
75
67
 
76
68
  # Returns the value of the property
77
- define_method "#{key}" do
69
+ define_method key.to_s do
78
70
  type = property_class.type(key)
79
71
  lookup_value(type, key)
80
72
  end
@@ -107,10 +99,10 @@ module PropertySets
107
99
  association_class.keys & keys.map(&:to_s)
108
100
  end
109
101
 
110
- property_pairs = property_keys.map do |name|
102
+ property_pairs = property_keys.flat_map do |name|
111
103
  value = lookup_value(association_class.type(name), name)
112
104
  [name, value]
113
- end.flatten(1)
105
+ end
114
106
  HashWithIndifferentAccess[*property_pairs]
115
107
  end
116
108
 
@@ -126,12 +118,12 @@ module PropertySets
126
118
  end
127
119
  end
128
120
 
129
- def save(*args)
130
- each { |p| p.save(*args) }
121
+ def save(...)
122
+ each { |p| p.save(...) }
131
123
  end
132
124
 
133
- def save!(*args)
134
- each { |p| p.save!(*args) }
125
+ def save!(...)
126
+ each { |p| p.save!(...) }
135
127
  end
136
128
 
137
129
  def protected?(arg)
@@ -147,7 +139,7 @@ module PropertySets
147
139
  end
148
140
 
149
141
  def build_default(arg)
150
- build(:name => arg.to_s, :value => association_class.raw_default(arg))
142
+ build(name: arg.to_s, value: association_class.raw_default(arg))
151
143
  end
152
144
 
153
145
  def lookup_without_default(arg)
@@ -157,7 +149,7 @@ module PropertySets
157
149
  def lookup_value(type, key)
158
150
  serialized = property_serialized?(key)
159
151
 
160
- if instance = lookup_without_default(key)
152
+ if (instance = lookup_without_default(key))
161
153
  instance.value_serialized = serialized
162
154
  PropertySets::Casting.read(type, instance.value)
163
155
  else
@@ -172,7 +164,7 @@ module PropertySets
172
164
 
173
165
  # The finder method which returns the property if present, otherwise a new instance with defaults
174
166
  def lookup(arg)
175
- instance = lookup_without_default(arg)
167
+ instance = lookup_without_default(arg)
176
168
  instance ||= build_default(arg)
177
169
  instance.value_serialized = property_serialized?(arg)
178
170
 
@@ -186,7 +178,7 @@ module PropertySets
186
178
  # It does not have the side effect of adding a new setting object.
187
179
  def lookup_or_default(arg)
188
180
  instance = lookup_without_default(arg)
189
- instance ||= association_class.new(:value => association_class.raw_default(arg))
181
+ instance ||= association_class.new(value: association_class.raw_default(arg))
190
182
  instance.value_serialized = property_serialized?(arg)
191
183
  instance
192
184
  end
@@ -201,18 +193,18 @@ module PropertySets
201
193
  update_property_set_attributes(attributes)
202
194
  super
203
195
  end
204
- alias update_attributes update
196
+ alias_method :update_attributes, :update
205
197
 
206
198
  def update!(attributes)
207
199
  update_property_set_attributes(attributes)
208
200
  super
209
201
  end
210
- alias update_attributes! update!
202
+ alias_method :update_attributes!, :update!
211
203
 
212
204
  def update_property_set_attributes(attributes)
213
205
  if attributes && self.class.property_set_index.any?
214
206
  self.class.property_set_index.each do |property_set|
215
- if property_set_hash = attributes.delete(property_set)
207
+ if (property_set_hash = attributes.delete(property_set))
216
208
  send(property_set).set(property_set_hash, true)
217
209
  end
218
210
  end
@@ -221,7 +213,7 @@ module PropertySets
221
213
 
222
214
  def update_columns(attributes)
223
215
  if delegated_property_sets?
224
- attributes = attributes.reject{|k,_| self.class.delegated_property_set_attributes.include?(k.to_s) }
216
+ attributes = attributes.reject { |k, _| self.class.delegated_property_set_attributes.include?(k.to_s) }
225
217
  end
226
218
 
227
219
  super attributes
@@ -248,7 +240,6 @@ module PropertySets
248
240
  attribute_names
249
241
  end
250
242
  end
251
-
252
243
  end
253
244
  end
254
245
 
@@ -1,8 +1,8 @@
1
- require 'json'
1
+ require "json"
2
2
 
3
3
  module PropertySets
4
4
  module Casting
5
- FALSE = [ "false", "0", "", "off", "n" ]
5
+ FALSE_VALUES = ["false", "0", "", "off", "n"]
6
6
 
7
7
  class << self
8
8
  def read(type, value)
@@ -53,7 +53,7 @@ module PropertySets
53
53
  private
54
54
 
55
55
  def false?(value)
56
- FALSE.include?(value.to_s.downcase)
56
+ FALSE_VALUES.include?(value.to_s.downcase)
57
57
  end
58
58
  end
59
59
  end
@@ -27,11 +27,7 @@ module PropertySets
27
27
 
28
28
  mappings.each do |old_attr, new_attr|
29
29
  self.delegated_property_set_attributes << old_attr.to_s
30
- if ActiveRecord.version < Gem::Version.new("5.0")
31
- attribute old_attr, ActiveRecord::Type::Value.new
32
- else
33
- attribute old_attr, ActiveModel::Type::Value.new
34
- end
30
+ attribute old_attr, ActiveModel::Type::Value.new
35
31
  define_method(old_attr) {
36
32
  association = send(setname)
37
33
  type = association.association_class.type(new_attr)
@@ -44,7 +40,7 @@ module PropertySets
44
40
  send("#{old_attr}_will_change!")
45
41
  end
46
42
  send(setname).send("#{new_attr}=", value)
47
- super(value) if defined?(super) # Rails 4 does not define this
43
+ super(value)
48
44
  end
49
45
 
50
46
  define_method("#{old_attr}_will_change!") do
@@ -69,7 +65,7 @@ module PropertySets
69
65
  # These are not database columns and should not be included in queries but
70
66
  # using the attributes API is the only way to track changes in the main model
71
67
  if respond_to?(:user_provided_columns)
72
- self.user_provided_columns.reject!{|k,_| delegated_property_set_attributes.include?(k.to_s) }
68
+ user_provided_columns.reject! { |k, _| delegated_property_set_attributes.include?(k.to_s) }
73
69
  end
74
70
  end
75
71
  end
@@ -1,22 +1,22 @@
1
- require 'active_support'
1
+ require "active_support"
2
2
 
3
3
  module PropertySets
4
4
  module PropertySetModel
5
5
  # https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html
6
6
  COLUMN_TYPE_LIMITS = {
7
- 'tinyblob' => 255, # 2^8 - 1
8
- 'tinytext' => 255,
9
- 'blob' => 65535, # 2^16 - 1
10
- 'text' => 65535,
11
- 'mediumblob' => 16777215, # 2^24 - 1
12
- 'mediumtext' => 16777215,
13
- 'longblob' => 4294967295, # 2^32 - 1
14
- 'longtext' => 4294967295,
7
+ "tinyblob" => 255, # 2^8 - 1
8
+ "tinytext" => 255,
9
+ "blob" => 65535, # 2^16 - 1
10
+ "text" => 65535,
11
+ "mediumblob" => 16777215, # 2^24 - 1
12
+ "mediumtext" => 16777215,
13
+ "longblob" => 4294967295, # 2^32 - 1
14
+ "longtext" => 4294967295
15
15
  }.freeze
16
16
 
17
17
  module InstanceMethods
18
18
  def false?
19
- [ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
19
+ ["false", "0", "", "off", "n"].member?(value.to_s.downcase)
20
20
  end
21
21
 
22
22
  def true?
@@ -93,7 +93,7 @@ module PropertySets
93
93
  end
94
94
 
95
95
  def value_column_limit
96
- column = self.class.columns_hash.fetch('value')
96
+ column = self.class.columns_hash.fetch("value")
97
97
 
98
98
  # use sql_type because type returns :text for all text types regardless of length
99
99
  column.limit || COLUMN_TYPE_LIMITS.fetch(column.sql_type)
@@ -102,9 +102,9 @@ module PropertySets
102
102
 
103
103
  module ClassMethods
104
104
  def self.extended(base)
105
- base.validate :validate_format_of_name
106
- base.validate :validate_length_of_serialized_data
107
- base.before_create :coerce_value
105
+ base.validate :validate_format_of_name
106
+ base.validate :validate_length_of_serialized_data
107
+ base.before_create :coerce_value
108
108
  base.attr_accessible :name, :value if defined?(ProtectedAttributes)
109
109
  end
110
110
 
@@ -137,11 +137,12 @@ module PropertySets
137
137
  end
138
138
 
139
139
  def owner_class=(owner_class_name)
140
- @owner_class_sym = owner_class_name.underscore.to_sym
141
- belongs_to owner_class_sym
142
- validates_presence_of owner_class_sym
143
- validates_uniqueness_of :name, :scope => owner_class_key_sym, :case_sensitive => false
144
- attr_accessible owner_class_key_sym, owner_class_sym if defined?(ProtectedAttributes)
140
+ @owner_class_sym = owner_class_name.to_s.demodulize.underscore.to_sym
141
+
142
+ belongs_to owner_class_sym, class_name: owner_class_name
143
+ validates_presence_of owner_class_sym, class_name: owner_class_name
144
+ validates_uniqueness_of :name, scope: owner_class_key_sym, case_sensitive: false
145
+ attr_accessible owner_class_key_sym, owner_class_sym if defined?(ProtectedAttributes)
145
146
  end
146
147
 
147
148
  def owner_assoc=(association)
@@ -160,6 +161,5 @@ module PropertySets
160
161
  "#{owner_class_sym}_id".to_sym
161
162
  end
162
163
  end
163
-
164
164
  end
165
165
  end
@@ -1,3 +1,3 @@
1
1
  module PropertySets
2
- VERSION = "3.8.0"
2
+ VERSION = "3.12.0"
3
3
  end
data/lib/property_sets.rb CHANGED
@@ -1,28 +1,41 @@
1
- require 'property_sets/property_set_model'
2
- require 'property_sets/active_record_extension'
3
- require 'property_sets/version'
1
+ require "property_sets/property_set_model"
2
+ require "property_sets/active_record_extension"
3
+ require "property_sets/version"
4
4
 
5
5
  begin
6
- require 'property_sets/action_view_extension'
6
+ require "property_sets/action_view_extension"
7
7
  rescue LoadError
8
8
  end
9
9
 
10
+ if "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" == "6.1"
11
+ ActiveRecord::Base.singleton_class.alias_method :connection_class_for_self, :connection_classes
12
+ end
13
+
10
14
  module PropertySets
11
15
  def self.ensure_property_set_class(association, owner_class_name)
12
- const_name = "#{owner_class_name}#{association.to_s.singularize.camelcase}".to_sym
16
+ const_name = "#{owner_class_name.demodulize}#{association.to_s.singularize.camelcase}"
17
+ namespace = owner_class_name.deconstantize.safe_constantize || Object
13
18
 
14
- unless Object.const_defined?(const_name)
15
- property_class = Class.new(ActiveRecord::Base) do
19
+ unless namespace.const_defined?(const_name, false)
20
+ property_class = Class.new(parent_for_property_class(namespace, owner_class_name)) do
16
21
  include PropertySets::PropertySetModel::InstanceMethods
17
- extend PropertySets::PropertySetModel::ClassMethods
22
+ extend PropertySets::PropertySetModel::ClassMethods
18
23
  end
19
24
 
20
- Object.const_set(const_name, property_class)
25
+ namespace.const_set(const_name, property_class)
21
26
 
22
27
  property_class.owner_class = owner_class_name
23
28
  property_class.owner_assoc = association
24
29
  end
25
30
 
26
- Object.const_get(const_name)
31
+ namespace.const_get(const_name.to_s)
32
+ end
33
+
34
+ def self.parent_for_property_class(namespace, owner_class_name)
35
+ owner_class = namespace.const_get(owner_class_name)
36
+
37
+ owner_class.property_sets_connection_class || owner_class.connection_class_for_self
38
+ rescue NameError
39
+ ::ActiveRecord::Base
27
40
  end
28
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: property_sets
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.0
4
+ version: 3.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Primdahl
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-17 00:00:00.000000000 Z
11
+ date: 2023-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '6.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.1'
22
+ version: '7.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '4.2'
29
+ version: '6.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.1'
32
+ version: '7.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: json
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -100,6 +100,20 @@ dependencies:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: standard
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
103
117
  - !ruby/object:Gem::Dependency
104
118
  name: byebug
105
119
  requirement: !ruby/object:Gem::Requirement
@@ -132,7 +146,7 @@ homepage: http://github.com/zendesk/property_sets
132
146
  licenses:
133
147
  - MIT
134
148
  metadata: {}
135
- post_install_message:
149
+ post_install_message:
136
150
  rdoc_options: []
137
151
  require_paths:
138
152
  - lib
@@ -140,15 +154,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
154
  requirements:
141
155
  - - ">="
142
156
  - !ruby/object:Gem::Version
143
- version: '2.4'
157
+ version: '2.7'
144
158
  required_rubygems_version: !ruby/object:Gem::Requirement
145
159
  requirements:
146
160
  - - ">="
147
161
  - !ruby/object:Gem::Version
148
162
  version: '0'
149
163
  requirements: []
150
- rubygems_version: 3.1.6
151
- signing_key:
164
+ rubygems_version: 3.0.3.1
165
+ signing_key:
152
166
  specification_version: 4
153
167
  summary: Property sets for ActiveRecord.
154
168
  test_files: []