Slide 1

Slide 1 text

The Dark Art of Rails Plugins James Adam reevoo.com

Slide 2

Slide 2 text

Anatomy of a plugin Photo: http://flickr.com/photos/guccibear2005/206352128/

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

lib

Slide 5

Slide 5 text

lib • added to the $LOAD_PATH • classes auto-loaded via Dependencies magic • order determined by config.plugins

Slide 6

Slide 6 text

init.rb

Slide 7

Slide 7 text

init.rb • evaluated near the end of rails initialization • evaluated in order of config.plugins • special variables available • config, directory, name - see source of Rails::Plugin

Slide 8

Slide 8 text

tasks [un]install.rb test generators

Slide 9

Slide 9 text

Writing Plugins

Slide 10

Slide 10 text

Sharing Code lib tasks

Slide 11

Slide 11 text

Adding new behaviour

Slide 12

Slide 12 text

What we’re aiming for class Person end class Programmer < Person end class ProjectManager < Person end p = Programmer.new p.hello # => "hi!" ProjectManager.is_friendly? # => true

Slide 13

Slide 13 text

Modules module Friendly def hello "hi from #{self}" end end

Slide 14

Slide 14 text

require 'friendly' class Person include Friendly end alice = Programmer.new alice.hello # => "hi from #"

Slide 15

Slide 15 text

Methods in classes... class Person def hello "hi from #{self}" end end

Slide 16

Slide 16 text

... and in modules module Friendly def hello "hi from #{self}" end end

Slide 17

Slide 17 text

Defining class methods... class Person def self.is_friendly? true end end

Slide 18

Slide 18 text

... and in modules? module Friendly def self.is_friendly? true end def hello "hi from #{self}" end end

Slide 19

Slide 19 text

Not quite :( class Person include Friendly end Person.is_friendly? # ~> undefined method `is_friendly?' for Person:Class (NoMethodError)

Slide 20

Slide 20 text

It’s all about self module Friendly def self.is_friendly? true end end Friendly.is_friendly? # => true

Slide 21

Slide 21 text

Try this instead module Friendly::ClassMethods def is_friendly? true end end class Person extend Friendly::ClassMethods end Person.is_friendly? # => true

Slide 22

Slide 22 text

Mixing in Modules class Person include AnyModule # adds to class definition end class Person extend AnyModule # adds to the object (self) end

Slide 23

Slide 23 text

Some other ways: Person.instance_eval do def greetings "hello via \ instance_eval" end end

Slide 24

Slide 24 text

Some other ways: class << Person def salutations "hello via \ class << Person" end end

Slide 25

Slide 25 text

module ActsAsFriendly module ClassMethods def is_friendly? true end end def hello "hi from #{self}!" end end Person.send(:include, ActsAsFriendly) Person.extend(ActsAsFriendly::ClassMethods)

Slide 26

Slide 26 text

included module B def self.included(base) puts "B included into #{base}!" end end class A include B end # => "B included into A!"

Slide 27

Slide 27 text

extended module B def self.extended(base) puts "#{base} extended by B!" end end class A extend B end # => "A extended by B!"

Slide 28

Slide 28 text

module ActsAsFriendly def self.included(base) base.extend(ClassMethods) end module ClassMethods def is_friendly? true end end def hello "hi from #{self}!" end end Person.send(:include, ActsAsFriendly)

Slide 29

Slide 29 text

module ActsAsFriendly def self.included(base) base.extend(ClassMethods) end module ClassMethods def is_friendly? true end end def hello "hi from #{self}!" end end Person.send(:include, ActsAsFriendly)

Slide 30

Slide 30 text

class EvilBoss < Person end EvilBoss.is_friendly? # => true

Slide 31

Slide 31 text

Showing restraint... • maybe we only want to apply it to particular classes • particularly if we’re going to change how the class behaves (see later...)

Slide 32

Slide 32 text

... using class methods • Ruby class definitions are code • So, has_many is a class method

Slide 33

Slide 33 text

Class methods! class BlogPost < ActiveRecord::Base has_many :comments validates_presence_of :title end

Slide 34

Slide 34 text

Class methods! class BlogController < ApplicationController before_filter :load_blog_posts end

Slide 35

Slide 35 text

Class methods! class Person attr_reader :name end

Slide 36

Slide 36 text

class Alpha puts self end # => Alpha Self in class definitions class SomeClass puts self end # >> SomeClass

Slide 37

Slide 37 text

Calling methods class SomeClass def self.greetings "hello" end greetings end # >> hello

Slide 38

Slide 38 text

module AbilityToFly def fly! true end # etc... end class Person def self.has_powers include AbilityToFly end end

Slide 39

Slide 39 text

class Villain < Person end class Hero < Person has_powers end clark_kent = Hero.new clark_kent.fly! # => true lex_luthor = Villain.new lex_luthor.fly! # => NoMethodError

Slide 40

Slide 40 text

Villain.has_powers lex.fly! # => true

Slide 41

Slide 41 text

module MyPlugin def acts_as_friendly include MyPlugin::ActsAsFriendly end module ActsAsFriendly def self.included(base) base.extend(ClassMethods) end module ClassMethods def is_friendly? true end end def hello "hi from #{self}" end end # of ActsAsFriendly end # of MyPlugin Person.extend(MyPlugin)

Slide 42

Slide 42 text

module MyPlugin def acts_as_friendly include MyPlugin::ActsAsFriendly end module ActsAsFriendly def self.included(base) base.extend(ClassMethods) end module ClassMethods def is_friendly? true end end def hello "hi from #{self}" end end # of ActsAsFriendly end # of MyPlugin Person.extend(MyPlugin)

Slide 43

Slide 43 text

module MyPlugin def acts_as_friendly include MyPlugin::ActsAsFriendly end module ActsAsFriendly def self.included(base) base.extend(ClassMethods) end module ClassMethods def is_friendly? true end end def hello "hi from #{self}" end end # of ActsAsFriendly end # of MyPlugin Person.extend(MyPlugin)

Slide 44

Slide 44 text

class Grouch < Person end oscar = Grouch.new oscar.hello # => NoMethodError class Hacker < Person acts_as_friendly end Hacker.is_friendly? # => true james = Hacker.new james.hello # => “hi from #”

Slide 45

Slide 45 text

Person.extend(MyPlugin)

Slide 46

Slide 46 text

ActiveRecord::Base.extend(MyPlugin)

Slide 47

Slide 47 text

Extending Rails

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

acts_as_archivable • when a record is deleted, save a YAML version. Just in case. • It’s an odd example, but bear with me.

Slide 50

Slide 50 text

Archivable Module module Archivable def archive_to_yaml File.open("#{id}.yml", 'w') do |f| f.write self.to_yaml end end end ActiveRecord::Base.send(:include, Archivable)

Slide 51

Slide 51 text

Redefine via a module module Archivable def archive_to_yaml File.open("#{id}.yml") # ...etc... end def destroy # redefine destroy! connection.delete %{ DELETE FROM #{table_name} WHERE id = #{self.id} } archive_to_yaml end end

Slide 52

Slide 52 text

Redefine via a module ActiveRecord::Base.send(:include, Archivable) class Thing < ActiveRecord::Base end t = Thing.find(:first) t.destroy # => no archive created :’(

Slide 53

Slide 53 text

Redefining in the class class ActiveRecord::Base def destroy # Actually delete the record connection.delete %{ DELETE FROM #{table_name} WHERE id = #{self.id} } # call our new method archive_to_yaml end end

Slide 54

Slide 54 text

...it’s evil naughty • ties our new functionality to ActiveRecord, in this example • maybe we want to add this to DataMapper? Or Sequel? Or Ambition? •What if Rails changes?

Slide 55

Slide 55 text

What we want • Our new behaviour should be triggered when destroy is called • The record should still be removed from the database • We shouldn’t need to reimplement the existing behaviour

Slide 56

Slide 56 text

alias_method def hello "hi!" end alias_method :greetings, :hello greetings # => "hi!"

Slide 57

Slide 57 text

alias_method alias_method :original_destroy, :destroy def new_destroy original_destroy archive_to_yaml end alias_method :destroy, :new_destroy

Slide 58

Slide 58 text

module Archivable alias_method :original_destroy, :destroy def new_destroy original_destroy archive_to_yaml end alias_method :destroy, :new_destroy end # ~> undefined method `destroy' for module `Archivable'

Slide 59

Slide 59 text

module Archivable def self.included(base) base.class_eval do alias_method :original_destroy, :destroy alias_method :destroy, :new_destroy end end def archive_to_yaml File.open("#{id}.yml") # ... end def new_destroy original_destroy archive_to_yaml end end ActiveRecord::Base.send(:include, Archivable)

Slide 60

Slide 60 text

class Thing < ActiveRecord::Base end t = Thing.find(:first) t.destroy # => archive created!

Slide 61

Slide 61 text

alias_method again alias_method :destroy_without_archiving, :destroy def destroy_with_archiving destroy_without_archiving # then add our new behaviour end alias_method :destroy, :destroy_with_archiving alias_method :destroy_without_archiving, :destroy def destroy_with_archiving destroy_without_archiving archive_to_yaml end alias_method :destroy, :destroy_with_archiving

Slide 62

Slide 62 text

alias_method_chain def destroy_with_archiving destroy_without_archiving archive_to_yaml end alias_method_chain :destroy, :archiving

Slide 63

Slide 63 text

module Archivable def self.included(base) base.class_eval do alias_method_chain :destroy, :archiving end end def archive_to_yaml File.open("#{id}.yml", "w") do |f| f.write self.to_yaml end end def destroy_with_archiving destroy_without_archiving archive_to_yaml end end ActiveRecord::Base.send(:include, Archivable)

Slide 64

Slide 64 text

So adding up everything • use extend to add class method • include the new behaviour by including a module when class method is called • use alias_method_chain to wrap existing method

Slide 65

Slide 65 text

module ActsAsArchivable def acts_as_archivable include ActsAsArchivable::Behaviour end module Behaviour def self.included(base) base.class_eval do alias_method_chain :destroy, :archiving end end def archive_to_yaml File.open("#{id}.yml") # ... end def destroy_with_archiving destroy_without_archiving archive_to_yaml end end end ActiveRecord::Base.extend(ActsAsArchivable)

Slide 66

Slide 66 text

module ActsAsArchivable def acts_as_archivable include ActsAsArchivable::Behaviour alias_method_chain :destroy, :archiving end module Behaviour def archive_to_yaml File.open("#{id}.yml") # ... end def destroy_with_archiving destroy_without_archiving archive_to_yaml end end end ActiveRecord::Base.extend(ActsAsArchivable)

Slide 67

Slide 67 text

module ActsAsArchivable def acts_as_archivable include ActsAsArchivable::Behaviour alias_method_chain :destroy, :archiving end module Behaviour def archive_to_yaml File.open("#{id}.yml") # ... end def destroy_with_archiving destroy_without_archiving archive_to_yaml end end end ActiveRecord::Base.extend(ActsAsArchivable)

Slide 68

Slide 68 text

class Thing < ActiveRecord::Base end t1 = Thing.create! t1.destroy # => normal destroy called Thing.count # => 0 Dir["*.yml"] # => [] class PreciousThing < ActiveRecord::Base acts_as_archivable end t2 = PreciousThing.create! t2.destroy PreciousThing.count # => 0 Dir["*.yml"] # => ["1.yml"]

Slide 69

Slide 69 text

Plugin Photo: http://flickr.com/photos/jreed/322057793/ Tips

Slide 70

Slide 70 text

Package your code • ...in a module • domain name, nickname, quirk module Lazyatom module ActsAsHasselhoff # ... end end Tip #1

Slide 71

Slide 71 text

Developing plugins • Dependencies.load_once_paths • config/environment/development.rb config.after_initialize do Dependencies.load_once_paths. delete_if do |path| path =~ /vendor\/plugins/ end end Tip #2

Slide 72

Slide 72 text

Gems as plugins • coming in Rails 2.1 (it’s in r9101) • add rails/init.rb to your gem • require “rubygems” in environment.rb Tip #3

Slide 73

Slide 73 text

Thanks! lazyatom.com/plugins