0

I have a module that sets a classes accessible attributes by looking for at their habtm relationships and belongs_to relationships. It also checks the table and adds their attributes to the accessible list.

The module looks like this

module ModAccessible
  extend ActiveSupport::Concern

  included do
    if table_exists?
      column_names.each {|att| attr_accessible att}
    end
    if reflect_on_all_associations(:has_and_belongs_to_many).any?
      association = reflect_on_all_associations(:has_and_belongs_to_many)
      association.each {|model| attr_accessible model.plural_name.singularize + "_id"}
      association.each {|model| attr_accessible model.plural_name}
    end
  end
end

The output of this i am tryig to test is like this..

Class

# == Schema Information
#
# Table name: accessibilitytypes
#
#  id         :integer          not null, primary key
#  title      :string(255)
#  displayit  :boolean
#  cssclass   :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class Accessibilitytype < ActiveRecord::Base

  has_and_belongs_to_many :venues

  validates :title, :presence=>true, :uniqueness=>true
  include ModAccessible  

end

Module NOT included

[35] pry(main)> Accessibilitytype.attr_accessible
=> {:default=>#<ActiveModel::MassAssignmentSecurity::WhiteList: {}>}

Module included

[37] pry(main)> Accessibilitytype.attr_accessible
=> {:default=>#<ActiveModel::MassAssignmentSecurity::WhiteList: {"id", "title", "displayit", "cssclass", "created_at", "updated_at", "venue_id", "venues"}>}

The test

I am trying to write a test but I am struggling to know how best to achieve it. I assume I should create a dummy class and not depend on real classes. But how do I establish relationships between the two classes in a test environment? How do I stub the attribute names in the absence of a table?

I'll give you a gist of what I am trying to achieve:

require 'spec_helper'
describe ModAccessible do 
    class Head < ActiveRecord::Base
        has_many :hairs
    end

    class Hair < ActiveRecord::Base
        belongs_to :head
    end
  before do 
    let(:dummy_class) {Hair.new { include MorseAccessible}}
  end

 describe "has_and_belongs_to_many" do 
    expect(dummy_class.attr_accessible).to include :head_id
 end

I hope this is clear. Thanks

user3868832
  • 610
  • 2
  • 9
  • 25

0 Answers0