Module Remarkable::ActiveRecord::Describe::ClassMethods
In: lib/remarkable_activerecord/describe.rb

Methods

Public Instance methods

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:
      active_record:
        describe:
          each: "{{key}} is {{value}}"
          prepend: "when "
          connector: " and "

See also subject_attributes instance and class methods for more information.

[Source]

     # File lib/remarkable_activerecord/describe.rb, line 108
108:         def describe(*args, &block)
109:           if described_class && args.first.is_a?(Hash)
110:             attributes = args.shift
111: 
112:             connector = Remarkable.t "remarkable.active_record.describe.connector", :default => " and "
113: 
114:             description = if self.describe_subject_attributes.blank?
115:               Remarkable.t("remarkable.active_record.describe.prepend", :default => "when ")
116:             else
117:               connector.lstrip
118:             end
119: 
120:             pieces = []
121:             attributes.each do |key, value|
122:               translated_key = if described_class.respond_to?(:human_attribute_name)
123:                 described_class.human_attribute_name(key.to_s, :locale => Remarkable.locale)
124:               else
125:                 key.to_s.humanize
126:               end
127: 
128:               pieces << Remarkable.t("remarkable.active_record.describe.each",
129:                                       :default => "{{key}} is {{value}}",
130:                                       :key => translated_key.downcase, :value => value.inspect)
131:             end
132: 
133:             description << pieces.join(connector)
134:             args.unshift(description)
135: 
136:             # Creates an example group, set the subject and eval the given block.

137:             #

138:             example_group = super(*args) do
139:               write_inheritable_hash(:describe_subject_attributes, attributes)
140:               set_described_subject!
141:               instance_eval(&block)
142:             end
143:           else
144:             super(*args, &block)
145:           end
146:         end

[Source]

     # File lib/remarkable_activerecord/describe.rb, line 166
166:         def set_described_subject!
167:           subject {
168:             record = self.class.described_class.new
169:             record.send(:attributes=, subject_attributes, false)
170:             record
171:           }
172:         end

Sets default attributes for the subject. You can use this to set up your subject with valid attributes. You can even mix with a fixture replacement tool and still use quick subjects:

  describe Post
    # Fixjour example
    subject_attributes { valid_post_attributes }

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

[Source]

     # File lib/remarkable_activerecord/describe.rb, line 161
161:         def subject_attributes(options=nil, &block)
162:           write_inheritable_attribute(:default_subject_attributes, options || block)
163:           set_described_subject!
164:         end

[Validate]