Created
December 6, 2012 00:50
-
-
Save christopherhein/4220953 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # lib/media_library/models/has_profile.rb | |
| module MediaLibrary | |
| module Models | |
| module HasProfile | |
| extend ActiveSupport::Concern | |
| # Define methods that get called on classes | |
| # that include file | |
| module ClassMethods | |
| # method to call on the models that have are profileable | |
| # | |
| # This can call any number of active record class methods | |
| # that are relevant to profileable. | |
| def has_profile | |
| has_one :profile, as: :profileable | |
| delegate :description,:title,:content_type, to: :profile | |
| accepts_nested_attributes_for :profile | |
| end | |
| end | |
| # Empty for now be cause I can't think of any necessary methods | |
| # right now that is. | |
| module InstanceMethods | |
| end | |
| end | |
| end | |
| end | |
| # Include the module in all of ActiveRecord::Base since we need this in every model | |
| ActiveRecord::Base.include(MediaLibrary::Models::Profileable) | |
| # Once this is all done in your models you can now use | |
| # | |
| # app/models/media_library/image.rb | |
| module MediaLibrary | |
| class Image < ActiveRecord::Base | |
| has_profile | |
| # … Omitted for awesomeness … | |
| end | |
| end | |
| # app/models/media_library/document.rb | |
| module MediaLibrary | |
| class Document < ActiveRecord::Base | |
| has_profile | |
| end | |
| end | |
| Good articles on using ActiveSupport::Concern http://chris-schmitz.com/extending-activemodel-via-activesupportconcern/ | |
| and http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment