Transcript
Page 1: Contributing To Rails By Plugin Gem

Contributing to Rails

with plugins or gems

Richard Huang

Ekohe

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 2: Contributing To Rails By Plugin Gem

AgendaTesting

Plugins

Gems

Contribute

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 3: Contributing To Rails By Plugin Gem

Why use plugins or gemsComponent

Robust

Multiple choices

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 4: Contributing To Rails By Plugin Gem

Why write a plugin or gemShare your ideas

DRY

Learn a lot

Make your code better

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Page 5: Contributing To Rails By Plugin Gem

Plugin

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Read the guides first

http://guides.rubyonrails.org/plugins.html

Read other people’s plugins

Just do it

Page 6: Contributing To Rails By Plugin Gem

Plugin structure

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Executed when plugin loaded

Executed when plugin installed

Executed when plugin destroyed

Rake task added to rails rake load path

Logic codes of your plugin

Test codes of your plugin

Define plugin rake tasks

Page 7: Contributing To Rails By Plugin Gem

Plugin structure

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Plugin logic codes

Textile README

require ‘bullet’

require ‘bulletware’reqire

‘bullet/association’……

Rspec to test plugins

Page 8: Contributing To Rails By Plugin Gem

Testing – the more, the better

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Tests are documents

Tests make your code robust

Tests enable good communication between you and contributors

Page 9: Contributing To Rails By Plugin Gem

Autotest

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

autotest

autospec

Page 10: Contributing To Rails By Plugin Gem

Testing ActiveRecord

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Use sqlite3 (memory)

Create and drop table each time

Silence your migration

Page 11: Contributing To Rails By Plugin Gem

Adding methods

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

class methods and instance methods

Page 12: Contributing To Rails By Plugin Gem

Changing method behavior

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

alias_method

Page 13: Contributing To Rails By Plugin Gem

Hack with different rails versions

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Implementations are different in different rails versions

Page 14: Contributing To Rails By Plugin Gem

Configure your plugin

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Enable or disable plugin

Page 15: Contributing To Rails By Plugin Gem

Lazy loading

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

autoload

Page 16: Contributing To Rails By Plugin Gem

Reuse logic

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Similar to writing rails app

–Model

–Controller

–Helper

–Task

Page 17: Contributing To Rails By Plugin Gem

README

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Let others know what you are doing

Rdoc, Textile or Markdown

An example is better

Page 18: Contributing To Rails By Plugin Gem

Gem

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Easier to maintain

Easier to upgrade

Includes dependencies automatically

Can use without rails

Page 19: Contributing To Rails By Plugin Gem

Gem tool -- Jeweler

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Defining your gem

Bumping gem version

Releasing gem

Page 20: Contributing To Rails By Plugin Gem

Adding gem rake

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Rails does not load gem rake

Load it yourself

Page 21: Contributing To Rails By Plugin Gem

Adding executable to gem

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Using optparse to get input

Defining executable in gemspec

Page 22: Contributing To Rails By Plugin Gem

Contribute

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Write down your ideas

Share your codes (rubyforge, github, gemcutter, etc.)

Tell others what you do

Use it and improve it

Page 23: Contributing To Rails By Plugin Gem

Contribute

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Don’t be afraid of reinventing the wheel

Don’t be afraid of ugly code

Don’t be afraid of bugs

Page 24: Contributing To Rails By Plugin Gem

To be a better contributor

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development

Read other people’s plugins

http://github.com/flyerhzm/monitor

Page 25: Contributing To Rails By Plugin Gem

Q&A

Thank you

Website: http://www.huangzhimin.com

Github: http://github.com/flyerhzm

www.ekohe.comWeb Development & Graphic DesignChina Ruby on Rails Development - Rails Consulting - Rails Services - Merb - Offshore Web Development


Recommended