tests pass for the account, content_entry and content_asset models

This commit is contained in:
Didier Lafforgue 2012-02-16 12:42:41 +01:00
parent 6619d4e5dc
commit 2fd0410b66
6 changed files with 66 additions and 53 deletions

View File

@ -86,7 +86,7 @@ module Locomotive
membership = site.memberships.where(:account_id => self._id).first
if site.admin_memberships.size == 1 && membership.admin?
raise I18n.t('errors.messages.needs_admin_account')
raise ::I18n.t('errors.messages.needs_admin_account')
else
membership.destroy
end

View File

@ -17,11 +17,12 @@ module Locomotive
validates :_slug, :presence => true, :uniqueness => { :scope => :content_type_id }
## associations ##
belongs_to :site
belongs_to :site, :class_name => 'Locomotive::Site'
belongs_to :content_type, :class_name => 'Locomotive::ContentType', :inverse_of => :entries
## callbacks ##
before_validation :set_slug
before_save :set_site
before_save :set_visibility
before_save :set_label_field_name
before_create :add_to_list_bottom
@ -45,10 +46,6 @@ module Locomotive
end
end
def visible?
self._visible || self._visible.nil?
end
def next
next_or_previous :gt
end
@ -86,9 +83,9 @@ module Locomotive
def next_or_previous(matcher = :gt)
order_by = self.content_type.order_by_definition
criterion = attribute.send(matcher)
criterion = :_position.send(matcher)
self.class.where(criterion => self.send(attribute)).order_by([order_by]).limit(1).first
self.class.where(criterion => self._position).order_by([order_by]).limit(1).first
end
# Sets the slug of the instance by using the value of the highlighted field
@ -115,12 +112,15 @@ module Locomotive
self.class.where(:_id.ne => self._id, :_slug => self._slug).any?
end
def set_visibility
[:visible, :active].each do |meth|
if self.respond_to?(meth)
self._visible = self.send(meth)
return
def set_site
self.site ||= self.content_type.site
end
def set_visibility
if self.respond_to?(:visible)
self.visible = true if self.visible.nil?
self._visible = self.visible
return
end
end
@ -135,8 +135,8 @@ module Locomotive
def send_notifications
return if !self.content_type.public_submission_enabled? || self.content_type.public_submission_accounts.blank?
self.content_type.site.accounts.each do |account|
next unless self.content_type.public_submission_accounts.include?(account._id.to_s)
self.site.accounts.each do |account|
next unless self.content_type.public_submission_accounts.include?(account._id)
Locomotive::Notifications.new_content_entry(account, self).deliver
end

View File

@ -113,6 +113,10 @@ module Locomotive
self.order_by ||= 'created_at'
self.label_field_id = self.entries_custom_fields.first._id if self.label_field_id.blank?
field = self.entries_custom_fields.find(self.label_field_id)
# the label field should always be required
field.required = true
self.label_field_name = field.name
end

View File

@ -32,8 +32,9 @@ describe Locomotive::Account do
it 'should own many sites' do
account = FactoryGirl.create(:account)
site_1 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
site_2 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
account.reload.sites.to_a.should == [site_1, site_2]
site_2 = FactoryGirl.create(:site, :subdomain => 'another_one', :memberships => [Locomotive::Membership.new(:account => account)])
sites = [site_1, site_2].map(&:_id)
account.reload.sites.all? { |s| sites.include?(s._id) }.should be_true
end
describe 'deleting' do

View File

@ -36,8 +36,8 @@ describe Locomotive::ContentAsset do
end
it 'has any possible resized versions' do
@asset.stubs(:with).returns(81)
@asset.stubs(:height).returns(81)
@asset.stubs(:with).returns(90)
@asset.stubs(:height).returns(90)
@asset.vignette_url.should =~ /^\/images\/dynamic\/.*\/5k.png/
end

View File

@ -7,13 +7,15 @@ describe Locomotive::ContentEntry do
before(:each) do
Locomotive::Site.any_instance.stubs(:create_default_pages!).returns(true)
@content_type = FactoryGirl.build(:content_type)
@content_type.entries_custom_fields.build :label => 'Title', :type => 'String'
@content_type.entries_custom_fields.build :label => 'Description', :type => 'Text'
@content_type.entries_custom_fields.build :label => 'Visible ?', :type => 'Text', :name => 'visible'
@content_type.highlighted_field_name = 'custom_field_1'
@content_type.entries_custom_fields.build :label => 'Title', :type => 'string'
@content_type.entries_custom_fields.build :label => 'Description', :type => 'text'
@content_type.entries_custom_fields.build :label => 'Visible ?', :type => 'boolean', :name => 'visible'
@content_type.valid?
@content_type.send(:set_default_values)
end
describe '#validation' do
it 'is valid' do
build_content_entry.should be_valid
end
@ -21,13 +23,13 @@ describe Locomotive::ContentEntry do
## Validations ##
it 'requires the presence of title' do
content_entry = build_content_entry_entry :title => nil
content_entry = build_content_entry :title => nil
content_entry.should_not be_valid
content_entry.errors[:title].should == ["can't be blank"]
end
it 'requires the presence of the permalink (_slug)' do
content_entry = build_content_entry_entry :title => nil
content_entry = build_content_entry :title => nil
content_entry.should_not be_valid
content_entry.errors[:_slug].should == ["can't be blank"]
end
@ -66,21 +68,23 @@ describe Locomotive::ContentEntry do
describe "#navigation" do
before(:each) do
@content_type.save
%w(first second third).each_with_index do |item, index|
content = build_content_entry({:title => item.to_s})
content._position_in_list = index
content = build_content_entry(:title => item.to_s, :_position => index)
content.save
instance_variable_set "@#{item}", content
end
end
it 'should find previous item when available' do
@second.previous.custom_field_1.should == "first"
@second.previous._position_in_list.should == 0
@second.previous.title.should == 'first'
@second.previous._position.should == 1
end
it 'should find next item when available' do
@second.next.custom_field_1.should == "third"
@second.next._position_in_list.should == 2
@second.next.title.should == 'third'
@second.next._position.should == 3
end
it 'should return nil when fetching previous item on first in list' do
@ -132,20 +136,20 @@ describe Locomotive::ContentEntry do
end
it 'is visible by default' do
@content_entry._visible?.should be_true
@content_entry.should be_visible
@content_entry.send(:set_visibility)
@content_entry.visible?.should be_true
end
it 'can be visible even if it is nil' do
@content_entry.visible = nil
@content_entry.send(:set_visibility).should be_true
@content_entry.should be_visible
@content_entry.send(:set_visibility)
@content_entry.visible?.should be_true
end
it 'can not be visible' do
@content_entry.visible = false
@content_entry.send(:set_visibility).should be_true
@content_entry.should_not be_visible
@content_entry.send(:set_visibility)
@content_entry.visible?.should be_false
end
end
@ -153,50 +157,54 @@ describe Locomotive::ContentEntry do
describe '#requirements' do
it 'has public access to the highlighted field value' do
build_content_entry.highlighted_field_value.should == 'Locomotive'
build_content_entry._label.should == 'Locomotive'
end
end
describe '#api' do
describe '#public_submission' do
before(:each) do
@account_1 = FactoryGirl.build('admin user', :id => fake_bson_id('1'))
@account_2 = FactoryGirl.build('frenchy user', :id => fake_bson_id('2'))
@content_type.api_enabled = true
@content_type.api_accounts = ['', @account_1.id, @account_2.id]
@content_type.public_submission_enabled = true
@content_type.public_submission_accounts = ['', @account_1._id, @account_2._id]
Locomotive::Site.any_instance.stubs(:accounts).returns([@account_1, @account_2])
site = FactoryGirl.build(:site)
site.stubs(:accounts).returns([@account_1, @account_2])
@content_entry = build_content_entry
@content_entry = build_content_entry(:site => site)
end
it 'does not send email notifications if the api is disabled' do
@content_type.api_enabled = false
@content_type.public_submission_enabled = false
Locomotive::Notifications.expects(:new_content_entry).never
@content_entry.save
end
it 'does not send email notifications if no api accounts' do
@content_type.api_accounts = nil
@content_type.public_submission_accounts = nil
Locomotive::Notifications.expects(:new_content_entry).never
@content_entry.save
end
it 'sends email notifications when a new instance is created' do
Locomotive::Notifications.expects(:new_content_entry).with(@account_1, @content).returns(mock('mailer', :deliver => true))
Locomotive::Notifications.expects(:new_content_entry).with(@account_2, @content).returns(mock('mailer', :deliver => true))
Locomotive::Notifications.expects(:new_content_entry).with(@account_1, @content_entry).returns(mock('mailer', :deliver => true))
Locomotive::Notifications.expects(:new_content_entry).with(@account_2, @content_entry).returns(mock('mailer', :deliver => true))
@content_entry.save
end
end
describe '#site' do
it 'delegates to the content type' do
@content_type.expects(:site)
build_content_entry.site
it 'assigns a site when saving the content entry' do
content_entry = build_content_entry
content_entry.save
content_entry.site.should_not be_nil
end
end
def build_content_entry(options = {})