diff options
author | Vijay Dev <vijaydev.cse@gmail.com> | 2012-08-04 15:18:35 +0530 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2012-08-04 15:18:35 +0530 |
commit | 3d3fa165e40c8cdaaebdab9dd2985d0ca3a36236 (patch) | |
tree | 21ae9b984d34c9487628097f3933f8fa14a3558f /guides/source/engines.textile | |
parent | c963f883a41913624363bfd8203b5640318198c2 (diff) | |
parent | b51201242aaf77c6db5a9b2f72e433c521df79c5 (diff) | |
download | rails-3d3fa165e40c8cdaaebdab9dd2985d0ca3a36236.tar.gz rails-3d3fa165e40c8cdaaebdab9dd2985d0ca3a36236.tar.bz2 rails-3d3fa165e40c8cdaaebdab9dd2985d0ca3a36236.zip |
Merge branch 'master' of github.com:lifo/docrails
Conflicts:
activemodel/lib/active_model/secure_password.rb
activerecord/lib/active_record/associations/collection_proxy.rb
Diffstat (limited to 'guides/source/engines.textile')
-rw-r--r-- | guides/source/engines.textile | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/guides/source/engines.textile b/guides/source/engines.textile index 53c2845731..11c837be32 100644 --- a/guides/source/engines.textile +++ b/guides/source/engines.textile @@ -657,6 +657,84 @@ h3. Improving engine functionality This section looks at overriding or adding functionality to the views, controllers and models provided by an engine. +h4. Overriding Models + +Engine Models can be extended by (1) implementing decorators, or (2) including modules. + +h5. Decorators + +Decorators extends Engine's model classes in the main application by open classing Engine models at run time execution. + +<ruby> +# MyApp/app/decorators/models/blorgh/post_decorator.rb + +Blorgh::Post.class_eval do + def time_since_created + Time.current - created_at + end +end +</ruby> + +h5. Modules + +The other strategy is to create modules within the Engine holding all the models' code and include these in the respective Engine's model classes. Thus the Engine's model classes contain a mere include line referencing the respective module. + +Engine models can be overriden in the main application by creating a file with the Engine's same namespace and including the module originally referenced in the Engine's model class. ["**ActiveSupport::Concern**":http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html] helps manage the correct ordering of module dependencies at run time (it's worth it to reach the linked documentation). + +<ruby> +# MyApp/app/models/blorgh/post.rb +# overrides Blorgh's original Post model + +class Blorgh::Post < ActiveRecord::Base + include Blorgh::Concerns::Models::Post + + def time_since_created + Time.current - created_at + end +end + + +# Blorgh/app/models/post.rb +# this class is overriden by the MyApp Post model + +class Post < ActiveRecord::Base + include Blorgh::Concerns::Models::Post +end + + +# Blorgh/app/concerns/models/post + +module Blorg::Concerns::Models::Post + extend ActiveSupport::Concern + + # 'included do' causes the code within to be evaluated in the conext + # where it is included, rather be executed in the module's context. + included do + attr_accessor :author_name + belongs_to :author, :class_name => "User" + + before_save :set_author + + private + + def set_author + self.author = User.find_or_create_by_name(author_name) + end + end + + # methods defined here will be instance methods by default + def some_method + 'some method string' + end + + module ClassMethods + def some_class_method + 'some class method string' + end + end +end +</ruby> + h4. Overriding views When Rails looks for a view to render, it will first look in the +app/views+ directory of the application. If it cannot find the view there, then it will check in the +app/views+ directories of all engines which have this directory. |