-
Notifications
You must be signed in to change notification settings - Fork 1
Creating a plugin
Recent changes to the codebase mean that FatFreeCRM plugins can now be developed as Rails Engines and included as gems. They can be used whether FatFreeCRM is running either as an engine itself (see Running-as-a-Rails-Engine) or as a standalone application (see Setup-Linux-or-Mac-OS or Setup-Microsoft-Windows).
Various plugins have already been updated to use the new engine-based plugin architecture. These include:
If you’re feeling adventurous, feel free to browse the source to get a feel for what is required.
The steps below essentially just create a rails engine and link it to FatFreeCRM. If you’d like to skip straight to the code or have a copy of this tutorial checked out to follow along, you can clone the ffcrm_awesome repo.
git clone git://github.com/fatfreecrm/ffcrm_awesome.git- Firstly, ensure you have a recent version of rails checked out. I’m writing this with v3.2.6, which is what FatFreeCRM is currently pegged to.
rails -v- Then, create a new rails plugin. The following line will create a new plugin called ffcrm_awesome, ensure ruby 1.8 compatible hashes are used and selects postgresql as the default database.
rails plugin new ffcrm_awesome --old-style-hash --full -d postgresql- Next, cd into your newly created plugin folder and start editting your Gemfile. Add
fat_free_crm(we’re using the git version as a gem hasn’t been released for a while).
gem 'fat_free_crm', :git => 'git://github.com/fatfreecrm/fat_free_crm.git'- Run
bundle installto include FatFreeCRM.
Here’s how to add a new controller action called ‘awesome’ that will apply to all your entities within FatFreeCRM.
- Inside your plugin, create a new file called {plugin_root}/lib/ffcrm_awesome/controllers.rb with the following text:
[Account, Campaign, Contact, Lead, Opportunity, Task].each do |model|
controller = (model.name.pluralize + 'Controller').constantize
controller.class_eval do
def awesome
# Insert awesome controller action code here
end
end
end- Next, ensure your controller hooks into the plugin initialisation process: augment the Engine class in {plugin_root}/lib/engines.rb with a config block as follows:
class Engine < Rails::Engine
config.to_prepare do
require 'ffcrm_awesome/controllers'
end
end- Configure your {plugin_root}/config/routes.rb file to include the new action:
Rails.application.routes.draw do
%w(accounts campaigns contacts leads opportunities tasks).each do |controller|
match "/#{controller}/awesome" => "#{controller}#awesome", :as => "#{controller}_awesome"
end
endTo test your plugin straight-away on your local FatFreeCRM instance, simply add your plugin to the Gemfile using the path where your gem resides.
For example:
gem 'ffcrm_awesome', :path => '/home/steve/rails/ffcrm_awesome'- Run
bundle installandrake routes | grep awesome. You should see your “awesome” method added to all the controllers as above.
Note: when you release your awesome gem, don’t forget to remove the :path part of the gem file above.
To enhance an existing FatFreeCRM model, one approach is to create a new module and then include it into the existing FatFreeCRM model.
- Create a new module in your plugin at
{plugin_root}/lib/ffcrm_awesome/awesomeness.rbwith the following code:
module FfcrmAwesome
module Awesomeness
def awesome
"Do something that makes FatFreeCRM awesome"
end
end
end- Now ensure your module is loaded and added to the Account entity. Extend @{plugin_root}/lib/engine.rb
module FfcrmAwesome
class Engine < ::Rails::Engine
config.to_prepare do
require 'ffcrm_awesome/controllers'
require 'ffcrm_awesome/awesomeness'
Account.class_eval do
include FfcrmAwesome::Awesomeness
end
end
end
end- Load your FatFreeCRM app in a
rails consoleand confirm that Accounts now have theawesome!method
>> Account.first.awesome
=> "Do something that makes FatFreeCRM awesome"Note: if you make changes to your plugin code you will need to restart your rails console. Simply typing reload will not reload the plugin code.
Combustion helps you test your rails engines by providing an interface to the parts of rails that you need in order to test. The github page contains detailed setup information and the steps below mainly come from there.
- Add
rspec-railsandcombustionto your plugin’s gemspec file
s.add_development_dependency 'rspec-rails', '~> 2.0'
s.add_development_dependency 'capybara', '~> 1.1.2'
s.add_development_dependency 'combustion', '~> 0.3.1'- Run
bundle installand initialiserspec
bundle install && rails generate rspec:install- Edit spec/spec_helper.rb to include
Combustion.initialize!. This must go beforerspec/railsandcapybara/rspec. The top of your file should start like this:
require 'rubygems' require 'bundler' require 'rails/all'Bundler.require :default, :developmentrequire 'capybara/rspec'Combustion.initialize!require 'rspec/rails' require 'rspec/autorun' require 'capybara/rails'... rest of spec_helper file goes here.
- Now initialize
combustionwhich will create a rails project in yourspec/internalfolder.
bundle exec combust- To ensure combustion uses your FatFreeCRM schema, copy your
db/schema.rbfile from your FatFreeCRM instance tospec/internal/db/schema.rb.
- Edit
spec/internal/config/database.ymlto point to your test db.
test:
adapter: postgresql
database: ffcrm_awesome_test
username:
password:
schema_search_path: public
min_messages: warning- Write a test for the
awesomecontroller method we created above. Call the filespec/routing/awesome_spec.rb.
require 'spec_helper'describe ContactsController do describe "routing" do it "generates awesome route for contacts" do { :get => "/contacts/awesome" }.should route_to(:controller => "contacts", :action => "awesome") end end end
- Run
rspec specand you should find you have a passing test.
Thanks for reading and hopefully now you can go and make FatFreeCRM even more awesome!