Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #78 from Shopify/uk-drop-gem-functionality
Browse files Browse the repository at this point in the history
Remove all functionality from this gem and bump to v3.0.0
  • Loading branch information
paracycle authored Apr 17, 2024
2 parents 62f9550 + 8bef538 commit 1edf57b
Show file tree
Hide file tree
Showing 29 changed files with 13 additions and 1,708 deletions.
37 changes: 0 additions & 37 deletions .github/workflows/ci.yml

This file was deleted.

5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Unreleased
3.0.0
-----

* Add Tapioca DSL compiler
* Drop support for Ruby 2.6
* This gem is now deprecated. The functionality of this gem has been merged into the [measured](https://github.com/Shopify/measured).

2.8.2
-----
Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
source 'https://rubygems.org'

gemspec

gem 'rails'
142 changes: 3 additions & 139 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,146 +1,10 @@
# Measured Rails [![Build Status](https://travis-ci.org/Shopify/measured-rails.svg)](https://travis-ci.org/Shopify/measured-rails) [![Gem Version](https://badge.fury.io/rb/measured-rails.svg)](http://badge.fury.io/rb/measured-rails)

This gem is the Rails integration for the [measured](https://github.com/Shopify/measured) gem.
Since version 3.0.0, the functionality of this gem has been merged into the [measured](https://github.com/Shopify/measured) gem. This gem no longer does anything useful and is not maintained.

It provides ActiveRecord adapter for persisting and retrieving measurements with their units, and model validations.
If you are using this gem, you should switch to using the `measured` gem directly.

## Installation

Using bundler, add to the Gemfile:

```ruby
gem 'measured-rails'
```

Or stand alone:

$ gem install measured-rails

## Usage

### ActiveRecord

Columns are expected to have the `_value` and `_unit` suffix, and be `DECIMAL` and `VARCHAR`, and defaults are accepted. Customizing the column used to hold units is supported, see below for details.

```ruby
class AddWeightAndLengthToThings < ActiveRecord::Migration
def change
add_column :things, :minimum_weight_value, :decimal, precision: 10, scale: 2
add_column :things, :minimum_weight_unit, :string, limit: 12

add_column :things, :total_length_value, :decimal, precision: 10, scale: 2, default: 0
add_column :things, :total_length_unit, :string, limit: 12, default: "cm"
end
end
```

A column can be declared as a measurement with its measurement subclass:

```ruby
class Thing < ActiveRecord::Base
measured Measured::Weight, :minimum_weight
measured Measured::Length, :total_length
measured Measured::Volume, :total_volume
end
```

You can optionally customize the model's unit column by specifying it in the `unit_field_name` option, as follows:

```ruby
class ThingWithCustomUnitAccessor < ActiveRecord::Base
measured_length :length, :width, :height, unit_field_name: :size_unit
measured_weight :total_weight, :extra_weight, unit_field_name: :weight_unit
measured_volume :total_volume, :extra_volume, unit_field_name: :volume_unit
end
```

Similarly, you can optionally customize the model's value column by specifying it in the `value_field_name` option, as follows:

```ruby
class ThingWithCustomValueAccessor < ActiveRecord::Base
measured_length :length, value_field_name: :custom_length
measured_weight :total_weight, value_field_name: :custom_weight
measured_volume :volume, value_field_name: :custom_volume
end
```

There are some simpler methods for predefined types:

```ruby
class Thing < ActiveRecord::Base
measured_weight :minimum_weight
measured_length :total_length
measured_volume :total_volume
end
```

This will allow you to access and assign a measurement object:

```ruby
thing = Thing.new
thing.minimum_weight = Measured::Weight.new(10, "g")
thing.minimum_weight_unit # "g"
thing.minimum_weight_value # 10
```

Order of assignment does not matter, and each property can be assigned separately and with mass assignment:

```ruby
params = { total_length_unit: "cm", total_length_value: "3" }
thing = Thing.new(params)
thing.total_length.to_s # 3 cm
```

### Validations

Validations are available:

```ruby
class Thing < ActiveRecord::Base
measured_length :total_length

validates :total_length, measured: true
end
```

This will validate that the unit is defined on the measurement, and that there is a value.

Rather than `true` the validation can accept a hash with the following options:

* `message`: Override the default "is invalid" message.
* `units`: A subset of units available for this measurement. Units must be in existing measurement.
* `greater_than`
* `greater_than_or_equal_to`
* `equal_to`
* `less_than`
* `less_than_or_equal_to`

All comparison validations require `Measured::Measurable` values, not scalars. Most of these options replace the `numericality` validator which compares the measurement/method name/proc to the column's value. Validations can also be combined with `presence` validator.

**Note:** Validations are strongly recommended since assigning an invalid unit will cause the measurement to return `nil`, even if there is a value:

```ruby
thing = Thing.new
thing.total_length_value = 1
thing.total_length_unit = "invalid"
thing.total_length # nil
```

## Tests

```
$ bundle exec rake test
```

## Contributing

1. Fork it ( https://github.com/Shopify/measured-rails/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Authors
## Original Authors

* [Kevin McPhillips](https://github.com/kmcphillips) at [Shopify](http://shopify.com/careers)
* [Sai Warang](https://github.com/cyprusad) at [Shopify](http://shopify.com/careers)
Expand Down
9 changes: 1 addition & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ require "bundler/gem_tasks"
require 'rake/testtask'

$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require "measured/rails/version"

task default: :test

desc 'Run the test stuite'
desc 'Run the test suite'
Rake::TestTask.new do |t|
t.libs << "test"
t.libs << "lib/**/*"
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
end

task tag: :build do
system "git commit -m'Released version #{ Measured::VERSION }' --allow-empty"
system "git tag -a v#{ Measured::VERSION } -m 'Tagging #{ Measured::VERSION }'"
system "git push --tags"
end
5 changes: 0 additions & 5 deletions gemfiles/rails-6.0.gemfile

This file was deleted.

5 changes: 0 additions & 5 deletions gemfiles/rails-6.1.gemfile

This file was deleted.

5 changes: 0 additions & 5 deletions gemfiles/rails-7.0.gemfile

This file was deleted.

5 changes: 0 additions & 5 deletions gemfiles/rails-edge.gemfile

This file was deleted.

5 changes: 1 addition & 4 deletions lib/measured-rails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# frozen_string_literal: true
require "measured/rails/base"

require "measured/rails/units/length"
require "measured/rails/units/weight"
require "measured/rails/units/volume"
# don't define anything, this gem is a no-op
96 changes: 0 additions & 96 deletions lib/measured/rails/active_record.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/measured/rails/base.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/measured/rails/railtie.rb

This file was deleted.

14 changes: 0 additions & 14 deletions lib/measured/rails/units/length.rb

This file was deleted.

Loading

0 comments on commit 1edf57b

Please sign in to comment.