Skip to content

RobinDaugherty/unitwise-rails

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Climate Circle CI

unitwise-rails is a gem that provides rails extension to the the refined unitwise gem.

Installation

Add this line to your application's Gemfile:

gem 'unitwise-rails', github: 'maximebedard/unitwise-rails'

Migrations

# Add the fields :protein_value and :protein_unit when the
# migration is executed.
class AddUnitToIngredients < ActiveRecord::Migration
  def self.up
    add_unit :ingredients, :protein
  end

  def self.down
    remove_unit :ingredients, :protein
  end
end

class CreateIngredients < ActiveRecord::Migration
  def change
    create_table :ingredients do |t|
      t.string :name
      t.unit :protein
      t.timestamps
    end
  end
end

Models

# Add a method #protein to the model that will always be converted to grams
class Ingredient < ActiveRecord::Base
  # Add unit with automatic conversion to grams before save
  unit_for :protein, convert_to: :gram

  # Add unit with compatibility validation
  unit_for :carbohydrate
  validates_unit_compatibility_of :carbohydrate, with: :gram
  validates_unit_presence_of :carbohydrate

  # or
  validates_unit :carbohydrate, presence: true, compatibility: :gram
end

TODOs

  • Add unit_for extension to ActiveRecord::Base
  • Add unit generation to migrations
  • Add validates_unit validator
  • Add validates_unit_compatibility_of validator
  • Add validates_unit_presence_of validator
  • Add Unitwise::Rails::Migration tests

About

Give rails proper unit manipulation.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages