Module: Remarkable::ActiveRecord::Describe::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#describe(*args, &block) ⇒ Object

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 "

See also subject_attributes instance and class methods for more information.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/remarkable/mongo_mapper/describe.rb', line 108

def describe(*args, &block)
  if described_class && args.first.is_a?(Hash)
    attributes = args.shift

    connector = Remarkable.t "remarkable.mongo_mapper.describe.connector", :default => " and "

    description = if self.describe_subject_attributes.blank?
      Remarkable.t("remarkable.mongo_mapper.describe.prepend", :default => "when ")
    else
      connector.lstrip
    end

    pieces = []
    attributes.each do |key, value|
      translated_key = if described_class.respond_to?(:human_attribute_name)
        described_class.human_attribute_name(key.to_s, :locale => Remarkable.locale)
      else
        key.to_s.humanize
      end

      pieces << Remarkable.t("remarkable.mongo_mapper.describe.each",
                              :default => "{{key}} is {{value}}",
                              :key => translated_key.downcase, :value => value.inspect)
    end

    description << pieces.join(connector)
    args.unshift(description)

    # Creates an example group, set the subject and eval the given block.
    #
    example_group = super(*args) do
      write_inheritable_hash(:describe_subject_attributes, attributes)
      set_described_subject!
      instance_eval(&block)
    end
  else
    super(*args, &block)
  end
end

#set_described_subject!Object



166
167
168
169
170
171
172
# File 'lib/remarkable/mongo_mapper/describe.rb', line 166

def set_described_subject!
  subject {
    record = self.class.described_class.new
    record.send(:attributes=, subject_attributes, false)
    record
  }
end

#subject_attributes(options = nil, &block) ⇒ Object

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


161
162
163
164
# File 'lib/remarkable/mongo_mapper/describe.rb', line 161

def subject_attributes(options=nil, &block)
  write_inheritable_attribute(:default_subject_attributes, options || block)
  set_described_subject!
end