Skip to content

Instantly share code, notes, and snippets.

@docunext
Last active December 15, 2015 08:39
Show Gist options
  • Save docunext/5232631 to your computer and use it in GitHub Desktop.
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.
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