-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
709 additions
and
773 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
.DS_Store |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
source :rubygems | ||
|
||
gem 'rails', '~> 3.0.0' | ||
gem 'rspec-rails', '~> 2.8.1' | ||
gem 'formtastic', '~> 1.2.4' | ||
gem 'rspec-rails', '~> 2.11.0' | ||
gem 'sqlite3-ruby', :require => 'sqlite3' | ||
gem 'json', '~> 1.6.5' | ||
gem "jeweler", "~> 1.6.2" | ||
gem 'json', '~> 1.6.5' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
source :rubygems | ||
|
||
gem 'rails', '~> 3.1.0' | ||
gem 'rspec-rails', '~> 2.8.1' | ||
gem 'formtastic', '~> 1.2.4' | ||
gem 'rspec-rails', '~> 2.11.0' | ||
gem 'sqlite3' | ||
gem 'json', '~> 1.6.5' | ||
gem "jeweler", "~> 1.6.2" | ||
gem 'json', '~> 1.6.5' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
source :rubygems | ||
|
||
gem 'rails', '~> 3.2.0' | ||
gem 'rspec-rails', '~> 2.8.1' | ||
gem 'formtastic', '~> 1.2.4' | ||
gem 'rspec-rails', '~> 2.11.0' | ||
gem 'sqlite3' | ||
gem 'json', '~> 1.6.5' | ||
gem "jeweler", "~> 1.6.2" | ||
gem 'json', '~> 1.6.5' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
module ClassyEnum | ||
autoload :Base, 'classy_enum/base' | ||
autoload :InstanceMethods, 'classy_enum/instance_methods' | ||
autoload :ClassMethods, 'classy_enum/class_methods' | ||
autoload :Attributes, 'classy_enum/attributes' | ||
end | ||
|
||
ActiveRecord::Base.send :extend, ClassyEnum::Attributes | ||
require 'classy_enum/collection' | ||
require 'classy_enum/conversion' | ||
require 'classy_enum/predicate' | ||
require 'classy_enum/base' | ||
require 'classy_enum/active_record' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module ClassyEnum | ||
module ActiveRecord | ||
|
||
# Class macro used to associate an enum with an attribute on an ActiveRecord model. | ||
# This method is automatically added to all ActiveRecord models when the classy_enum gem | ||
# is installed. Accepts an argument for the enum class to be associated with | ||
# the model. ActiveRecord validation is automatically added to ensure | ||
# that a value is one of its pre-defined enum members. | ||
# | ||
# ==== Example | ||
# # Associate an enum Priority with Alarm model's priority attribute | ||
# class Alarm < ActiveRecord::Base | ||
# classy_enum_attr :priority | ||
# end | ||
# | ||
# # Associate an enum Priority with Alarm model's alarm_priority attribute | ||
# classy_enum_attr :alarm_priority, :enum => 'Priority' | ||
# | ||
# # Allow enum value to be nil | ||
# classy_enum_attr :priority, :allow_nil => true | ||
# | ||
# # Allow enum value to be blank | ||
# classy_enum_attr :priority, :allow_blank => true | ||
def classy_enum_attr(attribute, options={}) | ||
enum = (options[:enum] || attribute).to_s.camelize.constantize | ||
allow_blank = options[:allow_blank] || false | ||
allow_nil = options[:allow_nil] || false | ||
serialize_as_json = options[:serialize_as_json] || false | ||
|
||
error_message = "must be #{enum.all.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ')}" | ||
|
||
# Add ActiveRecord validation to ensure it won't be saved unless it's an option | ||
validates_inclusion_of attribute, | ||
:in => enum.all, | ||
:message => error_message, | ||
:allow_blank => allow_blank, | ||
:allow_nil => allow_nil | ||
|
||
# Define getter method that returns a ClassyEnum instance | ||
define_method attribute do | ||
enum.build(read_attribute(attribute), | ||
:owner => self, | ||
:serialize_as_json => serialize_as_json, | ||
:allow_blank => (allow_blank || allow_nil) | ||
) | ||
end | ||
|
||
# Define setter method that accepts either string or symbol for member | ||
define_method "#{attribute}=" do |value| | ||
value = value.to_s unless value.nil? | ||
super(value) | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
ActiveRecord::Base.send :extend, ClassyEnum::ActiveRecord |
Oops, something went wrong.