Module: Remarkable::ActiveRecord::Describe

Defined in:
lib/remarkable/mongo_mapper/describe.rb

Overview

Overwrites describe to provide quick way to configure your subject:

describe Post
  should_validate_presente_of :title

  describe :published => true do
    should_validate_presence_of :published_at
  end
end

This is the same as:

describe Post
  should_validate_presente_of :title

  describe "when published is true" do
    subject { Post.new(:published => true) }
    should_validate_presence_of :published_at
  end
end

The string can be localized using I18n. An example yml file is:

locale:
  remarkable:
    mongo_mapper:
      describe:
        each: "{{key}} is {{value}}"
        prepend: "when "
        connector: " and "

You can also call subject attributes to set the default attributes for a subject. You can even mix with a fixture replacement tool:

describe Post
  # Fixjour example
  subject_attributes { valid_post_attributes }

  describe :published => true do
    should_validate_presence_of :published_at
  end
end

You can retrieve the merged result of all attributes given using the subject_attributes instance method:

describe Post
  # Fixjour example
  subject_attributes { valid_post_attributes }

  describe :published => true do
    it "should have default subject attributes" do
      subject_attributes.should == { :title => 'My title', :published => true }
    end
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



68
69
70
# File 'lib/remarkable/mongo_mapper/describe.rb', line 68

def self.included(base) #:nodoc:
  base.extend ClassMethods
end

Instance Method Details

#subject_attributesObject

Returns a hash with the subject attributes declared using the subject_attributes class method and the attributes given using the describe method.

describe Post
  subject_attributes { valid_post_attributes }

  describe :published => true do
    it "should have default subject attributes" do
      subject_attributes.should == { :title => 'My title', :published => true }
    end
  end
end


189
190
191
192
193
194
195
# File 'lib/remarkable/mongo_mapper/describe.rb', line 189

def subject_attributes
  default = self.class.default_subject_attributes
  default = self.instance_eval(&default) if default.is_a?(Proc)
  default ||= {}

  default.merge(self.class.describe_subject_attributes || {})
end