Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I got things to work on Rails 3.1.0.rc1 with Ruby 1.9.2, by following <a href="http://guides.rubyonrails.org/plugins.html" rel="nofollow">"The Basics of Creating Rails Plugins"</a> Rails Guide and correcting the code whenever it exploded due to an incompatibility.</p> <p>I started by running:</p> <pre><code>Code$ rails new tester Code$ cd tester tester$ rails generate plugin foobar --with-generator </code></pre> <p>Then modifying the generated code to obtain these files in addition to the default ones:</p> <pre><code># vendor/plugins/foobar/init.rb require 'foobar' </code></pre> <hr> <pre><code># vendor/plugins/foobar/lib/foobar.rb %w{ models controllers helpers }.each do |dir| path = File.join(File.dirname(__FILE__), 'app', dir) $LOAD_PATH &lt;&lt; path ActiveSupport::Dependencies.autoload_paths &lt;&lt; path ActiveSupport::Dependencies.autoload_once_paths.delete(path) end # I'm not entirely sure this is the best way to add our plugin's views # to the view search path, but it works ActionController::Base.view_paths = ActionController::Base.view_paths + [ File.join(File.dirname(__FILE__), 'app', 'views') ] </code></pre> <hr> <pre><code>&lt;!-- vendor/plugins/foobar/lib/app/views/things/index.html.erb --&gt; &lt;h1&gt;Listing things&lt;/h1&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;% @things.each do |thing| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= thing.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Destroy', thing, confirm: 'Are you sure?', method: :delete %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; </code></pre> <hr> <pre><code># vendor/plugins/foobar/test/database.yml sqlite: :adapter: sqlite :dbfile: vendor/plugins/foobar/test/foobar_plugin.sqlite.db sqlite3: :adapter: sqlite3 :database: vendor/plugins/foobar/test/foobar_plugin.sqlite3.db postgresql: :adapter: postgresql :username: postgres :password: postgres :database: foobar_plugin_test :min_messages: ERROR mysql: :adapter: mysql :host: localhost :username: root :password: password :database: foobar_plugin_test </code></pre> <hr> <pre><code># vendor/plugins/foobar/test/schema.rb ActiveRecord::Schema.define(:version =&gt; 0) do create_table :things, :force =&gt; true do |t| t.string :name t.datetime :created_at t.datetime :updated_at end end </code></pre> <hr> <pre><code># vendor/plugins/foobar/test/test_helper.rb ENV['RAILS_ENV'] = 'test' ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..' require 'test/unit' require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb')) def load_schema config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") db_adapter = ENV['DB'] # no db passed, try one of these fine config-free DBs before bombing. db_adapter ||= begin require 'rubygems' require 'sqlite' 'sqlite' rescue MissingSourceFile begin require 'sqlite3' 'sqlite3' rescue MissingSourceFile end end if db_adapter.nil? raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3." end ActiveRecord::Base.establish_connection(config[db_adapter]) load(File.dirname(__FILE__) + "/schema.rb") require File.dirname(__FILE__) + '/../init' end load_schema </code></pre> <hr> <pre><code># vendor/plugins/foobar/test/foobar_test.rb require File.dirname(__FILE__) + '/test_helper' # create the test model class Thing &lt; ActiveRecord::Base end # create the test controller, which renders the included index template class ThingsController &lt; ActionController::Base def index @things = Thing.all respond_to do |format| format.html # index.html.erb end end def destroy @thing = Thing.find(params[:id]) @thing.destroy respond_to do |format| format.html { redirect_to things_url } end end end # confirm that the test environment is working correctly class ThingsTest &lt; ActiveSupport::TestCase test "schema has loaded correctly" do assert_equal [], Thing.all end test "model is loaded correctly" do assert_kind_of Thing, Thing.new end end # confirm that the controller and routes are working correctly class ThingsControllerTest &lt; ActionController::TestCase test "should load index" do with_routing do |set| set.draw do resources :things, :only =&gt; [:index, :destroy] end get :index assert_response :success end end end </code></pre> <p>And finally, our test passes:</p> <pre><code>tester$ cd vendor/plugins/foobar/ foobar$ rake -- create_table(:things, {:force=&gt;true}) -&gt; 0.0059s -- initialize_schema_migrations_table() -&gt; 0.0002s -- assume_migrated_upto_version(0, ["db/migrate"]) -&gt; 0.0003s Loaded suite /Users/nick/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/rake_test_loader Started ... Finished in 0.091642 seconds. 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload