Skip to content

Commit

Permalink
Merge pull request #9 from shivam091/1.6.0
Browse files Browse the repository at this point in the history
1.6.0
  • Loading branch information
shivam091 authored Dec 10, 2023
2 parents f40550d + 59475e3 commit 6beabf8
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 127 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [1.6.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.5.0...v1.6.0) - 2023-12-10

### What's new

- Added tests for `measured_*` methods.

-----------

## [1.5.0](https://github.com/shivam091/unit_measurements-rails/compare/v1.4.0...v1.5.0) - 2023-11-27

### What's new
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
unit_measurements-rails (1.5.0)
unit_measurements-rails (1.6.0)
activemodel (>= 7)
activerecord (>= 7)
railties (>= 7)
Expand Down
43 changes: 27 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A Rails adaptor that encapsulate measurements and their units in Ruby on Rails.
## Introduction

This gem is designed as a Rails integration for the [unit_measurements](https://github.com/shivam091/unit_measurements) gem.
It provides an `ActiveRecord` adapter for persisting and retrieving measurements along with their units, simplifying complex
It provides an `ActiveRecord` adapter for persisting and retrieving measurement quantity along with its unit, simplifying complex
measurement handling within your Rails applications.

## Minimum Requirements
Expand Down Expand Up @@ -42,8 +42,8 @@ Or otherwise simply install it yourself as:

### ActiveRecord

This gem provides an ActiveRecord integration allowing you to declare measurement attributes with their
corresponding units in your database schema:
This gem provides an ActiveRecord integration allowing you to declare measurement
attributes along with their corresponding units in your database schema:

```ruby
class CreateCubes < ActiveRecord::Migration[7.0]
Expand Down Expand Up @@ -76,6 +76,9 @@ cube.length_unit #=> "ft"
cube.length #=> 5.0 ft
```

Attribute accessor names are expected to have the `_quantity` and `_unit` suffix,
and be `DECIMAL` and `VARCHAR` types, respectively, and defaults values are accepted.

You can specify multiple measurement attributes simultaneously:

```ruby
Expand All @@ -84,28 +87,36 @@ class Cube < ActiveRecord::Base
end
```

Attribute names are expected to have the `_quantity` and `_unit` suffix, and be
`DECIMAL` and `VARCHAR` types, respectively, and defaults values are accepted.

You can customize the model's quantity and unit accessors by specifying them in the
`quantity_attribute_name` and `unit_attribute_name` options, respectively.
You can customize the quantity and unit accessors of the measurement attribute by
specifying them in the `quantity_attribute_name` and `unit_attribute_name` options,
respectively.

```ruby
class CubeWithCustomAccessor < ActiveRecord::Base
measured_length :length, unit_attribute_name: :length_uom
measured_length :width, quantity_attribute_name: :width_value
measured UnitMeasurements::Length, :length, :width, :height, unit_attribute_name: :size_uom
measured UnitMeasurements::Weight, :weight, quantity_attribute_name: :width_quantity
end
```

For a more streamlined approach, predefined methods are available for commonly used
types like `length`, `weight`, `area`, `volume`, etc.:
types:

| # | Unit group | Method name |
| :- | :---------- | :----------- |
| 1 | Length or distance | measured_length |
| 2 | Weight or mass | measured_weight |
| 3 | Time or duration | measured_time |
| 4 | Temperature | measured_temperature |
| 5 | Area | measured_area |
| 6 | Volume | measured_volume |
| 7 | Density | measured_density |

```ruby
class Package < ActiveRecord::Base
class CubeWithPredefinedMethods < ActiveRecord::Base
measured_length :size
measured_area :carpet_area
measured_volume :total_volume
measured_weight :item_weight, :package_weight
measured_volume :volume
measured_weight :weight
measured_density :density
end
```

Expand All @@ -121,4 +132,4 @@ Contributions to this project are welcomed! To contribute:

## License

Copyright 2023 [Harshal V. LADHE]((https://shivam091.github.io)), Released under the [MIT License](http://opensource.org/licenses/MIT).
Copyright 2023 [Harshal V. LADHE](https://shivam091.github.io), Released under the [MIT License](http://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion lib/unit_measurements/rails/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
module UnitMeasurements
module Rails
# Current stable version.
VERSION = "1.5.0"
VERSION = "1.6.0"
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@
# Establish connection to the database and dump database.
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
require_relative "support/schema.rb"

# Load shared examples
require_relative "support/shared_examples"
9 changes: 0 additions & 9 deletions spec/support/models/container.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/models/land.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/models/package.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/models/project_timeline.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/models/substance.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/models/weather_report.rb

This file was deleted.

56 changes: 1 addition & 55 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 1) do
ActiveRecord::Schema.define(version: 2) do
create_table "cubes", force: :cascade do |t|
t.decimal "length_quantity", precision: 10, scale: 2
t.string "length_unit", limit: 12
Expand All @@ -28,58 +28,4 @@
t.decimal "height_value", precision: 10, scale: 2
t.string "height_uom", limit: 12
end

create_table "containers", force: :cascade do |t|
t.decimal "total_volume_quantity", precision: 10, scale: 2
t.string "total_volume_unit", limit: 12
t.decimal "internal_volume_quantity", precision: 10, scale: 2
t.string "internal_volume_unit", limit: 12
t.decimal "external_volume_quantity", precision: 10, scale: 2
t.string "external_volume_unit", limit: 12
end

create_table "lands", force: :cascade do |t|
t.decimal "total_area_quantity", precision: 10, scale: 2
t.string "total_area_unit", limit: 12
t.decimal "carpet_area_quantity", precision: 10, scale: 2
t.string "carpet_area_unit", limit: 12
t.decimal "buildup_area_quantity", precision: 10, scale: 2
t.string "buildup_area_unit", limit: 12
end

create_table "packages", force: :cascade do |t|
t.decimal "total_weight_quantity", precision: 10, scale: 2
t.string "total_weight_unit", limit: 12
t.decimal "item_weight_quantity", precision: 10, scale: 2
t.string "item_weight_unit", limit: 12
t.decimal "package_weight_quantity", precision: 10, scale: 2
t.string "package_weight_unit", limit: 12
end

create_table "substances", force: :cascade do |t|
t.decimal "total_density_quantity", precision: 10, scale: 2
t.string "total_density_unit", limit: 12
t.decimal "internal_density_quantity", precision: 10, scale: 2
t.string "internal_density_unit", limit: 12
t.decimal "external_density_quantity", precision: 10, scale: 2
t.string "external_density_unit", limit: 12
end

create_table "weather_reports", force: :cascade do |t|
t.decimal "average_temperature_quantity", precision: 10, scale: 2
t.string "average_temperature_unit", limit: 12
t.decimal "day_temperature_quantity", precision: 10, scale: 2
t.string "day_temperature_unit", limit: 12
t.decimal "night_temperature_quantity", precision: 10, scale: 2
t.string "night_temperature_unit", limit: 12
end

create_table "project_timelines", force: :cascade do |t|
t.decimal "duration_quantity", precision: 10, scale: 2
t.string "duration_unit", limit: 12
t.decimal "setup_time_quantity", precision: 10, scale: 2
t.string "setup_time_unit", limit: 12
t.decimal "processing_time_quantity", precision: 10, scale: 2
t.string "processing_time_unit", limit: 12
end
end
65 changes: 65 additions & 0 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/shared_examples.rb

RSpec.shared_examples "measured method" do |unit_group, method_name|
let!(:mock_model) { Class.new(ActiveRecord::Base) }

it "responds to .#{method_name}" do
expect(mock_model).to respond_to(method_name)
end

describe ".#{method_name}" do
it "defines single measurement attribute" do
mock_model.public_send(method_name, :attribute)

expect_attribute_definition("attribute", unit_group, "attribute_quantity", "attribute_unit")
end

it "defines multiple measurement attributes" do
mock_model.public_send(method_name, :attribute1, :attribute2)

expect_attribute_definition("attribute1", unit_group, "attribute1_quantity", "attribute1_unit")
expect_attribute_definition("attribute2", unit_group, "attribute2_quantity", "attribute2_unit")
end

it "accepts string or symbol for measurement attribute name" do
mock_model.public_send(method_name, "string_attr", :symbol_attr)

expect_attribute_definition("string_attr", unit_group, "string_attr_quantity", "string_attr_unit")
expect_attribute_definition("symbol_attr", unit_group, "symbol_attr_quantity", "symbol_attr_unit")
end

it "accepts quantity_attribute_name and unit_attribute_name options" do
mock_model.public_send(method_name, :attribute1, quantity_attribute_name: :custom_quantity, unit_attribute_name: :custom_unit)

expect_attribute_definition("attribute1", unit_group, "custom_quantity", "custom_unit")
end

it "accepts string or symbol for quantity_attribute_name and unit_attribute_name options" do
mock_model.public_send(method_name, :attribute1, quantity_attribute_name: "custom_quantity", unit_attribute_name: :custom_unit)

expect_attribute_definition("attribute1", unit_group, "custom_quantity", "custom_unit")
end

it "allows using both quantity_attribute_name and unit_attribute_name options simultaneously" do
mock_model.public_send(method_name, :attribute1, quantity_attribute_name: :custom_quantity, unit_attribute_name: :custom_unit)

expect_attribute_definition("attribute1", unit_group, "custom_quantity", "custom_unit")
end
end

private

def expect_attribute_definition(attribute, unit_group, quantity_attribute_name, unit_attribute_name)
expect(mock_model.measured_attributes).to have_key(attribute)

attribute_info = mock_model.measured_attributes[attribute]

expect(attribute_info[:unit_group]).to eq(unit_group)
expect(attribute_info[:quantity_attribute_name]).to eq(quantity_attribute_name)
expect(attribute_info[:unit_attribute_name]).to eq(unit_attribute_name)
end
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/area_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/area_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Area do
include_examples "measured method", UnitMeasurements::Area, :measured_area
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/density_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/density_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Density do
include_examples "measured method", UnitMeasurements::Density, :measured_density
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/length_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/length_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Length do
include_examples "measured method", UnitMeasurements::Length, :measured_length
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/temperature_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/temperature_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Temperature do
include_examples "measured method", UnitMeasurements::Temperature, :measured_temperature
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/time_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/time_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Time do
include_examples "measured method", UnitMeasurements::Time, :measured_time
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/volume_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/volume_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Volume do
include_examples "measured method", UnitMeasurements::Volume, :measured_volume
end
9 changes: 9 additions & 0 deletions spec/unit_measurements/rails/unit_groups/weight_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

# spec/unit_measurements/rails/unit_groups/weight_spec.rb

RSpec.describe UnitMeasurements::Rails::ActiveRecord::Weight do
include_examples "measured method", UnitMeasurements::Weight, :measured_weight
end

0 comments on commit 6beabf8

Please sign in to comment.