简体   繁体   中英

rails: how to supply a model with gem, and how to test rails gem without dummy app

I have already read The Basics Of Creating Rails Plugins and several other articles, but I can't find how to supply a model with a gem.

Say, I want to make a gem for tagging (yes, I know about acts_as_taggable_on gem, I need different functionality). So, I want the model Tag to be bundled in the gem. I found no tutorial explaining that.

Of course I tried to reverse-engineer acts_as_taggable_on gem to understand how does it work, but it brought even more confusion: the tutorial I mentioned above says that I should have a dummy app in my gem, in order to test the gem. BUT, acts_as_taggable_on has no such dummy application! How how does it get tested, then?

About the model: ok, I see the file lib/acts_as_taggable_on/tag.rb that seems to be a Tag model:

module ActsAsTaggableOn
  class Tag < ::ActiveRecord::Base
    # ..........................
  end
end

I see that file lib/acts-as-taggable-on.rb requires tag :

require "acts_as_taggable_on/tag"

So I've applied the same approach (assume my plugin is named dftags) :

I have added file lib/dftags/tag.rb :

module Dftags
  class Tag < ::ActiveRecord::Base
    # attr_accessible :title, :body
  end
end

And my lib/dftags.rb looks like this:

module Dftags
end

require "dftags/tag"

I have specs tag_spec.rb :

require 'spec_helper'

describe Tag do
  let(:tag) { Tag.new(name: "") }
  it { should validate_presence_of :name }
end

And when I run bundle exec rspec spec/ , I got error unitialized constant Tag (NameError) .

It seems I missed something important. Plus, again, I have dummy app for testing, but acts_as_taggable_on doesn't; so, the testing approach should be different..

So, the questions:

  • How can I supply a model with gem?
  • How can I test my gem without dummy app?
  • Are there some advanced docs about writing rails gems? Actually I tried to check out one more famous gem: devise , but the ruby-fu and rails-fu of the authors is too strong for me to understand it. Where do people learn all of it?

How can I test my gem without dummy app?

The dummy app is only a helper that allows you to use your normal rails testing workflow when building a gem / plugin. You could run the tests without a dummy app but you would need a lot more manual work.

acts_as_taggable_on is pretty much active_record only with the exception of a single helper (as far as I can tell from a quick glance). The author therefore decided that the overhead of maintaining the dummy app was not worth the effort and is setting up active_record by hand. See here https://github.com/mbleigh/acts-as-taggable-on/blob/master/spec/spec_helper.rb#L24 how he establishes the connection to the database.

This code would not be necessary when using a dummy app as rails is taking care of it.

The same is true for the helper. Instead of using the test methods provided by rails he creates a new Class that includes the helper and uses an instance of this class to test it (h/acts-as-taggable-on/blob/master/spec/acts_as_taggable_on/tags_helper_spec.rb#L11).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM