Created
January 16, 2010 15:19
-
-
Save ddemaree/278853 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
class Ando::Item | |
include MongoMapper::Document | |
set_collection_name "items" | |
def self.model_name | |
model_name = super | |
model_name.instance_eval do | |
@singular = "ando_items".freeze | |
@plural = "ando_item".freeze | |
@collection = "items".freeze | |
# Note that I'm setting my own @collection, but NOT @element | |
@partial_path = "#{@collection}/#{@element}".freeze | |
end | |
model_name | |
end | |
end |
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
Ando::BlogPost.model_name #=> "blog_post" | |
# Look at all these useful variations! | |
Ando::BlogPost.model_name.singular #=> "ando_blog_post" | |
Ando::BlogPost.model_name.plural #=> "ando_blog_posts" | |
Ando::BlogPost.model_name.collection #=> "ando/blog_posts" | |
Ando::BlogPost.model_name.element #=> "blog_post" | |
Ando::BlogPost.model_name.partial_path #=> "blog_posts/blog_post" |
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 ActiveSupport | |
class ModelName < String | |
attr_reader :singular, :plural, :element, :collection, :partial_path | |
alias_method :cache_key, :collection | |
def initialize(name) | |
super | |
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze | |
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze | |
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze | |
@collection = ActiveSupport::Inflector.tableize(self).freeze | |
@partial_path = "#{@collection}/#{@element}".freeze | |
end | |
end | |
module CoreExtensions | |
module Module | |
# Returns an ActiveSupport::ModelName object for module. It can be | |
# used to retrieve all kinds of naming-related information. | |
def model_name | |
@model_name ||= ::ActiveSupport::ModelName.new(name) | |
end | |
end | |
end | |
end |
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
# BlogPost is a subclass of ActiveRecord::Base | |
@blog_post = BlogPost.find(108) | |
# Same as render "blog_posts/blog_post", :object => @blog_post | |
render @blog_post | |
# Will call blog_post_url(@blog_post.id) | |
url_for @blog_post #=> "/blog_posts/108" | |
# Lots of Rails's tag/form helpers use this | |
dom_id @blog_post #=> "blog_post_108" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment