Last active
April 5, 2017 17:37
-
-
Save alexch/a7be54e1b085718473ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for use by collection_select and friends, to link a human-readable label with a db-friendly symbolic value | |
# todo: ActiveRecord macros for setters (for allowing multiple values or just one) | |
# Usage: | |
# Table name: snacks | |
# id :integer | |
# ice_cream :string | |
# class Snack < ActiveRecord::Base | |
# FLAVORS = Enum.new [ | |
# [:vanilla, "Vanilla"], | |
# [:chocolate, "Chocolate"], | |
# [:rocky_road, "Rocky Road"] | |
# ]) | |
# ] | |
# end | |
# ... | |
# <%= simple_form_for @snack do |f| %> | |
# <%= f.collection_select :ice_cream, Snack::FLAVORS, :value, :label %> | |
# <% end %> | |
class Enum | |
class Item | |
attr_reader :value, :label | |
def initialize value, label | |
@value, @label = value, label | |
end | |
def ==(other) | |
other.is_a?(Enum::Item) and | |
other.value == value and | |
other.label == label | |
end | |
end | |
include Enumerable | |
attr_reader :items | |
def initialize item_tuples | |
@items = item_tuples.map do |item_tuple| | |
Item.new(item_tuple[0], item_tuple[1]) | |
end | |
end | |
def each &block | |
@items.each &block | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
describe Enum do | |
describe Enum::Item do | |
subject { Enum::Item.new(:rocky_road, "Rocky Road") } | |
it 'has a human-readable label' do | |
expect(subject.label).to eq("Rocky Road") | |
end | |
it 'has a symbolic value' do | |
expect(subject.value).to eq(:rocky_road) | |
end | |
end | |
describe "can be initialized with a list of item tuples" do | |
subject { Enum.new([ | |
[:vanilla, "Vanilla"], | |
[:chocolate, "Chocolate"], | |
[:rocky_road, "Rocky Road"] | |
]) } | |
it "contains the items" do | |
expect(subject.items[0]).to eq(Enum::Item.new(:vanilla, "Vanilla")) | |
expect(subject.items[1]).to eq(Enum::Item.new(:chocolate, "Chocolate")) | |
expect(subject.items[2]).to eq(Enum::Item.new(:rocky_road, "Rocky Road")) | |
end | |
it "implements enumerable (with each)" do | |
array = subject.items.dup | |
subject.each do |x| | |
expect(x).to eq(array.shift) | |
end | |
end | |
end | |
end |
@geekjimbo the best place would be /lib
folder. If you're running older version of Rails also make sure the lib
folder is in the autoload paths.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, txs for the advise.
Question: where do i put the Enum.rb within rails directory tree ?