unit_measurements-rails 1.4.0 → 1.5.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: 8df42aea1bd4eeeb476acbf102accaa55c206886b581df3f70e055accae95d41
4
- data.tar.gz: 8f1d81756ce2844917278e2c16f49665be362a427c3c1af3c00d8cc834b96f2b
3
+ metadata.gz: 88cfa69f2d7ec7ba60a19ac6a4519f38d928ae4cff9df64d72035e76b21cc975
4
+ data.tar.gz: 3bad903fa5c6fbc6f0404be8b85dc96a369ff3bb49a29229c8d57c4927835010
5
5
  SHA512:
6
- metadata.gz: e36f8d160d0644c4c66bd5723cdabef25db28ca5cae522dff70cb4299ea59c942d5190d0badc2374456b59db07dca09ac4f7c51bb22f8be261f67c932322cc79
7
- data.tar.gz: 8285500f4713be2467e28cb6a2e39605478babf072935a2a93f6de01dfe3338010fed463a094828f425d76a4c8c48124c09b68f1cdbb9ff9f13f3abbad2e5cb7
6
+ metadata.gz: c8bdc95a6136ee7d39ea0eb55c2ba3420632e7be2511c15eb7fca96b1083687005c2c08359bb0ee2bc5508070239b0dd60f1e5c588f150efd6b224ab40f1f260
7
+ data.tar.gz: 409dc3a9e99814361e1d97953118dfae1b4971da9c16da5bac8d77c81e4cd907a9d303aa883a22b040bb19f799291df5ef74644c9f21987c9c997599bc9e0f0e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [1.5.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.4.0...v1.5.0) - 2023-11-27
2
+
3
+ ### What's new
4
+
5
+ - Raise error when columns for measurement attribute don't exist in DB.
6
+
7
+ -----------
8
+
1
9
  ## [1.4.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.3.0...v1.4.0) - 2023-11-25
2
10
 
3
11
  ### What's updated
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements-rails (1.4.0)
4
+ unit_measurements-rails (1.5.0)
5
5
  activemodel (>= 7)
6
6
  activerecord (>= 7)
7
7
  railties (>= 7)
@@ -163,7 +163,7 @@ GEM
163
163
  simplecov_json_formatter (~> 0.1)
164
164
  simplecov-html (0.12.3)
165
165
  simplecov_json_formatter (0.1.4)
166
- sqlite3 (1.6.8-x86_64-linux)
166
+ sqlite3 (1.6.9-x86_64-linux)
167
167
  thor (1.3.0)
168
168
  timeout (0.4.1)
169
169
  tzinfo (2.0.6)
@@ -184,7 +184,7 @@ DEPENDENCIES
184
184
  rake (~> 13.0)
185
185
  rspec (~> 3.0)
186
186
  simplecov (~> 0.21, >= 0.21.2)
187
- sqlite3 (~> 1.6)
187
+ sqlite3 (~> 1.6.9)
188
188
  unit_measurements-rails!
189
189
 
190
190
  BUNDLED WITH
@@ -140,6 +140,8 @@ module UnitMeasurements
140
140
  # @since 1.0.0
141
141
  def define_reader_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
142
142
  define_method(measured_attr) do
143
+ column_exists?(quantity_attr) && column_exists?(unit_attr)
144
+
143
145
  quantity, unit = public_send(quantity_attr), public_send(unit_attr)
144
146
 
145
147
  begin
@@ -165,6 +167,8 @@ module UnitMeasurements
165
167
  # @since 1.0.0
166
168
  def define_writer_for_measured_attr(measured_attr, quantity_attr, unit_attr, unit_group)
167
169
  define_method("#{measured_attr}=") do |measurement|
170
+ column_exists?(quantity_attr) && column_exists?(unit_attr)
171
+
168
172
  if measurement.is_a?(unit_group)
169
173
  public_send("#{quantity_attr}=", measurement.quantity)
170
174
  public_send("#{unit_attr}=", measurement.unit.name)
@@ -187,6 +191,8 @@ module UnitMeasurements
187
191
  # @since 1.0.0
188
192
  def redefine_quantity_writer(quantity_attr)
189
193
  redefine_method("#{quantity_attr}=") do |quantity|
194
+ column_exists?(quantity_attr)
195
+
190
196
  quantity = BigDecimal(quantity, Float::DIG) if quantity.is_a?(String)
191
197
  if quantity
192
198
  db_column_props = self.column_for_attribute(quantity_attr)
@@ -212,6 +218,8 @@ module UnitMeasurements
212
218
  # @since 1.0.0
213
219
  def redefine_unit_writer(unit_attr, unit_group)
214
220
  redefine_method("#{unit_attr}=") do |unit|
221
+ column_exists?(unit_attr)
222
+
215
223
  unit_name = unit_group.unit_for(unit).try!(:name)
216
224
  write_attribute(unit_attr, (unit_name || unit))
217
225
  end
@@ -33,6 +33,7 @@ module UnitMeasurements
33
33
  end
34
34
  end
35
35
 
36
+ require "unit_measurements/rails/helpers"
36
37
  require "unit_measurements/rails/active_record"
37
38
 
38
39
  require "unit_measurements/rails/railtie" if defined?(Rails)
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ module Rails
7
+ # The +UnitMeasurements::Rails::Helpers+ module provides helper methods for
8
+ # handling +ActiveRecord+ models in the context of unit measurements.
9
+ #
10
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
11
+ # @since 1.5.0
12
+ module Helpers
13
+ # Checks the existence of a +column_name+ within the database table
14
+ # associated with the +ActiveRecord+ model.
15
+ #
16
+ # @param [String] column_name The name of the column to check for existence.
17
+ #
18
+ # @raise [ArgumentError]
19
+ # If the specified column does not exist in the database table.
20
+ #
21
+ # @example Check if the +height_quantity+ column exists in the database table:
22
+ # column_exists?("height_quantity")
23
+ #
24
+ # @return [void]
25
+ #
26
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
27
+ # @since 1.5.0
28
+ def column_exists?(column_name)
29
+ unless self.class.column_names.include?(column_name)
30
+ raise ArgumentError, "Column '#{column_name}' does not exist in the database for table '#{self.class.table_name}'."
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # ActiveSupport hook to include ActiveRecord with the `UnitMeasurements::Rails::Helpers`
38
+ # module.
39
+ ActiveSupport.on_load(:active_record) do
40
+ ::ActiveRecord::Base.send(:include, UnitMeasurements::Rails::Helpers)
41
+ end
@@ -5,6 +5,6 @@
5
5
  module UnitMeasurements
6
6
  module Rails
7
7
  # Current stable version.
8
- VERSION = "1.4.0"
8
+ VERSION = "1.5.0"
9
9
  end
10
10
  end
@@ -43,7 +43,7 @@ Gem::Specification.new do |spec|
43
43
  spec.add_development_dependency "rspec", "~> 3.0"
44
44
  spec.add_development_dependency "simplecov", "~> 0.21", ">= 0.21.2"
45
45
  spec.add_development_dependency "byebug", "~> 11"
46
- spec.add_development_dependency "sqlite3", "~> 1.6"
46
+ spec.add_development_dependency "sqlite3", "~> 1.6.9"
47
47
 
48
48
  spec.required_ruby_version = ">= 3.2"
49
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unit_measurements-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-25 00:00:00.000000000 Z
11
+ date: 2023-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -134,14 +134,14 @@ dependencies:
134
134
  requirements:
135
135
  - - "~>"
136
136
  - !ruby/object:Gem::Version
137
- version: '1.6'
137
+ version: 1.6.9
138
138
  type: :development
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: '1.6'
144
+ version: 1.6.9
145
145
  description: A Rails adaptor that encapsulate measurements and their units in Ruby
146
146
  on Rails.
147
147
  email:
@@ -167,6 +167,7 @@ files:
167
167
  - lib/unit_measurements-rails.rb
168
168
  - lib/unit_measurements/rails/active_record.rb
169
169
  - lib/unit_measurements/rails/base.rb
170
+ - lib/unit_measurements/rails/helpers.rb
170
171
  - lib/unit_measurements/rails/railtie.rb
171
172
  - lib/unit_measurements/rails/unit_groups/all.rb
172
173
  - lib/unit_measurements/rails/unit_groups/area.rb