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 membership = site.memberships.where(:account_id => self._id).first
if site.admin_memberships.size == 1 && membership.admin? 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 else
membership.destroy membership.destroy
end end

View File

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

View File

@ -113,6 +113,10 @@ module Locomotive
self.order_by ||= 'created_at' self.order_by ||= 'created_at'
self.label_field_id = self.entries_custom_fields.first._id if self.label_field_id.blank? 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) 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 self.label_field_name = field.name
end end

View File

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

View File

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

View File

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