ismasan (owner)

Forks

Revisions

gist: 28885 Download_button fork
public
Public Clone URL: git://gist.github.com/28885.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module BlahPlugin
  VERSION = '1.0.0'
  
  class << self
    # add plugin to AR only if it hasn't been included before,
    # which may cause stack-level-too-deep errors if you're aliasing methods
    #
    def enable_activerecord
      return if ActiveRecord::Base.respond_to? :acts_as_blah
      ActiveRecord::Base.extend BlahPlugin::Macro
    end
  end
 
  module Macro
    # his class level method injects the rest of the plugin behaviour in the affected class.
    # use like this:
    # class Post < ActiveRecord::Base
    # acts_as_blah
    # end
    #
    def acts_as_blah(opts = {:some_option => true}
      # don't include modules twice
      return if self.included_modules.include?(BlahPlugin::InstanceMethods)
      # include class and instanc methods
      extend BlahPlugin::ClassMethods
      include BlahPlugin::InstanceMethods
      # do something else, like initializing options
    end
  end
 
  module ClassMethods
    # class methods injected by the plugin, alias_method chaining, macro calls (has_many, after_save, validates),etc.
  end
 
  module InstanceMethods
    # instance methods injected by the plugin. callbacks, etc.
  end
  
end
 
require 'rubygems'
require 'active_record'
# enable the thing!
BlahPlugin.enable_activerecord