Created
April 23, 2010 17:41
-
-
Save anr/376868 to your computer and use it in GitHub Desktop.
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
# config/initializers/extensions.rb | |
ActiveRecord::Base.send(:include, StatusField) | |
ActiveRecord::Base.send(:include, SlugUrl) | |
# lib/status_field.rb | |
module StatusField | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def status_field(*args) | |
args.each do |arg| | |
define_method("status_#{arg}?") { status == arg.to_s } | |
define_method("status_#{arg}") { self.status = arg.to_s } | |
end | |
end | |
end | |
end | |
# lib/slug_url.rb | |
module SlugUrl | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def slug_url(attr) | |
define_method("to_param") { send(attr) } | |
instance_eval %{ | |
def find(*args, &block) | |
if block_given? || args.length != 1 | |
super | |
else | |
find_by_#{attr}!(args[0]) | |
end | |
end | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment