Last active
December 15, 2015 08:39
-
-
Save docunext/5232631 to your computer and use it in GitHub Desktop.
A snibbet of docunext/svxbox/lib/svxbox/roary.rb as an example of ActiveSupport::Concern.
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
module SvxBox::Roary | |
extend ActiveSupport::Concern | |
included do | |
cattr_accessor :param_suffix | |
before_validation :ensure_param_key_has_value, :if => :param_key_attribute? | |
validates :param_key, :presence => true, :uniqueness => true, :if => :param_key_attribute? | |
end | |
# ... | |
# class Something < ActiveRecord::Base | |
# include SvxBox::Roary | |
# to_param_suffix :name | |
# | |
# end | |
module ClassMethods | |
# ... | |
def find(key) | |
return super if !self.attribute_method?(:param_key) | |
begin | |
where("param_key = ?", key.to_s).first! | |
rescue ActiveRecord::RecordNotFound | |
super | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment