This commit is contained in:
did 2011-11-21 02:28:12 +01:00
commit 0b7cc6ebd2
41 changed files with 297 additions and 215 deletions

15
Gemfile
View File

@ -6,22 +6,18 @@ gem 'rake', '0.9.2'
gem 'rails', '~> 3.1.1' gem 'rails', '~> 3.1.1'
gem 'devise', :git => 'git://github.com/plataformatec/devise.git' gem 'devise', :git => 'git://github.com/plataformatec/devise.git'# FIXME: waiting for the new devise gem coming soon
# gem 'devise', '~> 1.4.9' # FIXME: waiting for the new devise gem coming soon
gem 'cancan', '~> 1.6.7' gem 'cancan', '~> 1.6.7'
gem 'bson', '~> 1.3.1' gem 'bson_ext', '~> 1.4.0'
gem 'mongo', '~> 1.3.1'
gem 'bson_ext', '~> 1.3.1'
gem 'mongoid', '~> 2.3.3' gem 'mongoid', '~> 2.3.3'
gem 'locomotive_mongoid_acts_as_tree', '0.1.5.7', :require => 'mongoid_acts_as_tree', :path => '../gems/acts_as_tree' # TODO: REPLACE IT gem 'locomotive_mongoid_acts_as_tree', :git => 'git@github.com:locomotivecms/mongoid_acts_as_tree.git'
gem 'custom_fields', :git => 'git://github.com/locomotivecms/custom_fields.git' gem 'custom_fields', :git => 'git://github.com/locomotivecms/custom_fields.git'
#gem 'custom_fields', '~> 1.1.0.rc1', :path => '../gems/custom_fields' gem 'kaminari'
gem 'will_paginate', '~> 3.0.2'
gem 'haml', '~> 3.1.3' gem 'haml', '~> 3.1.3'
gem 'sass-rails', '~> 3.1.4' gem 'sass-rails', '~> 3.1.4'
gem "compass", :git => 'git://github.com/chriseppstein/compass.git', :branch => 'rails31' gem "compass", :git => 'git://github.com/chriseppstein/compass.git'
gem 'coffee-script', '~> 2.2.0' gem 'coffee-script', '~> 2.2.0'
gem 'uglifier', '~> 1.0.4' gem 'uglifier', '~> 1.0.4'
gem 'jquery-rails', '~> 1.0.16' gem 'jquery-rails', '~> 1.0.16'
@ -82,7 +78,6 @@ group :test do
gem 'capybara' gem 'capybara'
gem 'database_cleaner' gem 'database_cleaner'
gem 'spork', '~> 0.9.0.rc'
gem 'launchy' gem 'launchy'
gem 'mocha', '0.9.12' # :git => 'git://github.com/floehopper/mocha.git' gem 'mocha', '0.9.12' # :git => 'git://github.com/floehopper/mocha.git'
end end

View File

@ -1,9 +1,8 @@
GIT GIT
remote: git://github.com/chriseppstein/compass.git remote: git://github.com/chriseppstein/compass.git
revision: 22e2458b77519e8eb8463170c1a1fe4bab105f3e revision: ebd758a7999a74611a3d682ba72c661a83f97498
branch: rails31
specs: specs:
compass (0.12.0.alpha.0.22e2458) compass (0.12.alpha.0.ebd758a)
chunky_png (~> 1.2) chunky_png (~> 1.2)
fssm (>= 0.2.7) fssm (>= 0.2.7)
sass (~> 3.1) sass (~> 3.1)
@ -27,7 +26,7 @@ GIT
GIT GIT
remote: git://github.com/plataformatec/devise.git remote: git://github.com/plataformatec/devise.git
revision: e8aabd465216bcb260448056c8ed4580d099a8c9 revision: a5aa03b98328d8e1fdb4081a74b3c04ff9593607
specs: specs:
devise (1.5.0.dev) devise (1.5.0.dev)
bcrypt-ruby (~> 3.0) bcrypt-ruby (~> 3.0)
@ -40,8 +39,9 @@ GIT
specs: specs:
handlebars-rails (0.9.1) handlebars-rails (0.9.1)
PATH GIT
remote: ../gems/acts_as_tree remote: git@github.com:locomotivecms/mongoid_acts_as_tree.git
revision: ca494d22c3d7946385aba1153c017d9c30e9f9d3
specs: specs:
locomotive_mongoid_acts_as_tree (0.1.5.7) locomotive_mongoid_acts_as_tree (0.1.5.7)
@ -87,8 +87,8 @@ GEM
autotest (4.4.6) autotest (4.4.6)
ZenTest (>= 4.4.1) ZenTest (>= 4.4.1)
bcrypt-ruby (3.0.1) bcrypt-ruby (3.0.1)
bson (1.3.1) bson (1.4.0)
bson_ext (1.3.1) bson_ext (1.4.0)
builder (3.0.0) builder (3.0.0)
bushido (0.0.35) bushido (0.0.35)
highline (>= 1.6.1) highline (>= 1.6.1)
@ -103,7 +103,7 @@ GEM
rack-test (>= 0.5.4) rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0) selenium-webdriver (~> 2.0)
xpath (~> 0.1.4) xpath (~> 0.1.4)
carrierwave (0.5.7) carrierwave (0.5.8)
activesupport (~> 3.0) activesupport (~> 3.0)
carrierwave-mongoid (0.1.3) carrierwave-mongoid (0.1.3)
carrierwave (>= 0.5.6) carrierwave (>= 0.5.6)
@ -120,10 +120,10 @@ GEM
execjs execjs
coffee-script-source (1.1.3) coffee-script-source (1.1.3)
columnize (0.3.4) columnize (0.3.4)
cucumber (1.1.2) cucumber (1.1.1)
builder (>= 2.1.2) builder (>= 2.1.2)
diff-lcs (>= 1.1.2) diff-lcs (>= 1.1.2)
gherkin (~> 2.6.2) gherkin (~> 2.6.0)
json (>= 1.4.6) json (>= 1.4.6)
term-ansicolor (>= 1.0.6) term-ansicolor (>= 1.0.6)
cucumber-rails (1.2.0) cucumber-rails (1.2.0)
@ -178,11 +178,13 @@ GEM
multi_json multi_json
multi_xml multi_xml
i18n (0.6.0) i18n (0.6.0)
jquery-rails (1.0.17) jquery-rails (1.0.16)
railties (~> 3.0) railties (~> 3.0)
thor (~> 0.14) thor (~> 0.14)
json (1.5.4) json (1.6.1)
json_pure (1.6.1) json_pure (1.6.1)
kaminari (0.12.4)
rails (>= 3.0.0)
kgio (2.6.0) kgio (2.6.0)
launchy (2.0.5) launchy (2.0.5)
addressable (~> 2.2.6) addressable (~> 2.2.6)
@ -197,8 +199,8 @@ GEM
mime-types (1.17.2) mime-types (1.17.2)
mimetype-fu (0.1.2) mimetype-fu (0.1.2)
mocha (0.9.12) mocha (0.9.12)
mongo (1.3.1) mongo (1.4.0)
bson (>= 1.3.1) bson (= 1.4.0)
mongoid (2.3.3) mongoid (2.3.3)
activemodel (~> 3.1) activemodel (~> 3.1)
mongo (~> 1.3) mongo (~> 1.3)
@ -284,7 +286,7 @@ GEM
archive-tar-minitar (>= 0.5.2) archive-tar-minitar (>= 0.5.2)
rubyzip (0.9.4) rubyzip (0.9.4)
sanitize (2.0.3) sanitize (2.0.3)
nokogiri (< 1.6, >= 1.4.4) nokogiri (>= 1.4.4, < 1.6)
sass (3.1.10) sass (3.1.10)
sass-rails (3.1.4) sass-rails (3.1.4)
actionpack (~> 3.1.0) actionpack (~> 3.1.0)
@ -297,11 +299,10 @@ GEM
ffi (= 1.0.9) ffi (= 1.0.9)
json_pure json_pure
rubyzip rubyzip
spork (0.9.0.rc9)
sprockets (2.0.3) sprockets (2.0.3)
hike (~> 1.2) hike (~> 1.2)
rack (~> 1.0) rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1) tilt (~> 1.1, != 1.3.0)
term-ansicolor (1.0.7) term-ansicolor (1.0.7)
thor (0.14.6) thor (0.14.6)
tilt (1.3.3) tilt (1.3.3)
@ -320,7 +321,6 @@ GEM
raindrops (~> 0.6) raindrops (~> 0.6)
warden (1.1.0) warden (1.1.0)
rack (>= 1.0) rack (>= 1.0)
will_paginate (3.0.2)
xpath (0.1.4) xpath (0.1.4)
nokogiri (~> 1.3) nokogiri (~> 1.3)
@ -333,8 +333,7 @@ DEPENDENCIES
ZenTest ZenTest
actionmailer-with-request (~> 0.3.0) actionmailer-with-request (~> 0.3.0)
autotest autotest
bson (~> 1.3.1) bson_ext (~> 1.4.0)
bson_ext (~> 1.3.1)
bushido (= 0.0.35) bushido (= 0.0.35)
cancan (~> 1.6.7) cancan (~> 1.6.7)
capybara capybara
@ -360,13 +359,13 @@ DEPENDENCIES
highline (~> 1.6.2) highline (~> 1.6.2)
httparty (~> 0.8.1) httparty (~> 0.8.1)
jquery-rails (~> 1.0.16) jquery-rails (~> 1.0.16)
kaminari
launchy launchy
linecache (= 0.43) linecache (= 0.43)
locomotive_liquid (= 2.2.2) locomotive_liquid (= 2.2.2)
locomotive_mongoid_acts_as_tree (= 0.1.5.7)! locomotive_mongoid_acts_as_tree!
mimetype-fu (~> 0.1.2) mimetype-fu (~> 0.1.2)
mocha (= 0.9.12) mocha (= 0.9.12)
mongo (~> 1.3.1)
mongoid (~> 2.3.3) mongoid (~> 2.3.3)
pickle pickle
rack-cache (~> 1.1) rack-cache (~> 1.1)
@ -382,10 +381,8 @@ DEPENDENCIES
rubyzip rubyzip
sanitize (~> 2.0.3) sanitize (~> 2.0.3)
sass-rails (~> 3.1.4) sass-rails (~> 3.1.4)
spork (~> 0.9.0.rc)
tinymce-rails tinymce-rails
uglifier (~> 1.0.4) uglifier (~> 1.0.4)
unicorn unicorn
uploadify-rails! uploadify-rails!
will_paginate (~> 3.0.2)
xpath (~> 0.1.4) xpath (~> 0.1.4)

View File

@ -3,8 +3,6 @@ module Locomotive
sections 'settings', 'site' sections 'settings', 'site'
actions :show, :new, :create
skip_load_and_authorize_resource skip_load_and_authorize_resource
before_filter :authorize_import before_filter :authorize_import
@ -23,7 +21,8 @@ module Locomotive
end end
end end
def new; end def new
end
def create def create
begin begin
@ -52,4 +51,4 @@ module Locomotive
end end
end end
end end

View File

@ -3,12 +3,13 @@ module Locomotive
sections 'settings', 'account' sections 'settings', 'account'
actions :edit, :update
respond_to :json, :only => :update respond_to :json, :only => :update
skip_load_and_authorize_resource skip_load_and_authorize_resource
def edit
end
def update def update
update! { edit_my_account_url } update! { edit_my_account_url }
end end

View File

@ -1,8 +1,6 @@
module Locomotive module Locomotive
class SitesController < BaseController class SitesController < BaseController
defaults :instance_name => 'site'
sections 'settings' sections 'settings'
def create def create

View File

@ -32,7 +32,7 @@ module Locomotive
can :touch, [Page, ThemeAsset] can :touch, [Page, ThemeAsset]
can :sort, Page can :sort, Page
can :manage, [ContentInstance, Asset] can :manage, [ContentInstance, ContentAsset]
can :touch, Site do |site| can :touch, Site do |site|
site == @site site == @site
@ -50,7 +50,7 @@ module Locomotive
can :manage, ThemeAsset can :manage, ThemeAsset
can :manage, Asset can :manage, ContentAsset
can :manage, Site do |site| can :manage, Site do |site|
site == @site site == @site
@ -82,4 +82,4 @@ module Locomotive
end end
end end
end end
end end

View File

@ -27,7 +27,7 @@ module Locomotive
end end
def reset_switch_site_token! def reset_switch_site_token!
self.switch_site_token = ActiveSupport::SecureRandom.base64(8).gsub("/", "_").gsub(/=+$/, "") self.switch_site_token = SecureRandom.base64(8).gsub("/", "_").gsub(/=+$/, "")
self.save self.save
end end
@ -37,7 +37,7 @@ module Locomotive
end end
def self.find_using_switch_site_token!(token, age = 1.minute) def self.find_using_switch_site_token!(token, age = 1.minute)
self.find_using_switch_site_token(token, age) || raise(Mongoid::Errors::DocumentNotFound.new(self, token)) self.find_using_switch_site_token(token, age) || raise(::Mongoid::Errors::DocumentNotFound.new(self, token))
end end
def devise_mailer def devise_mailer
@ -67,4 +67,4 @@ module Locomotive
end end
end end
end end

View File

@ -5,7 +5,11 @@ module Locomotive
include ::Mongoid::Timestamps include ::Mongoid::Timestamps
## extensions ## ## extensions ##
include CustomFields::ProxyClassEnabler #
# FIXME: I cannot find the module below defined in custom fields or the
# locomotive CMS engine. Please uncomment when the module is available
#
# include CustomFields::ProxyClassEnabler
include Extensions::Shared::Seo include Extensions::Shared::Seo
## fields (dynamic fields) ## ## fields (dynamic fields) ##
@ -124,4 +128,4 @@ module Locomotive
end end
end end
end end

View File

@ -144,4 +144,4 @@ module Locomotive
end end
end end
end end

View File

@ -1 +0,0 @@
# require 'will_paginate/array'

View File

@ -0,0 +1,8 @@
Feature: Has Many Association
As a designer
In order to make dealing with models easier
I want to be able to display other models that have a has many association
Scenario: Paginating a has many association
Given I have a site set up
Then I should be able to view a paginaed list of a has many association

View File

@ -0,0 +1,8 @@
Feature: Pagination
As a designer
In order to display a sensible amount of items per page
I want to be able to paginate through models
Scenario: Paginating through a model
Given I have a site set up
Then I should be able to display paginated models

View File

@ -66,4 +66,3 @@ end
Then %r{^I should see once the "([^"]*)" field$} do |field| Then %r{^I should see once the "([^"]*)" field$} do |field|
page.should have_css("#content_#{field.underscore.downcase}_input", :count => 1) page.should have_css("#content_#{field.underscore.downcase}_input", :count => 1)
end end

View File

@ -0,0 +1,61 @@
Given %r{^I have an? "([^"]*)" model which has many "([^"]*)"$} do |parent_model, child_model|
@parent_model = FactoryGirl.build(:content_type, :site => @site, :name => parent_model).tap do |ct|
ct.content_custom_fields.build :label => 'Body', :kind => 'string', :required => false
ct.save!
end
@child_model = FactoryGirl.build(:content_type, :site => @site, :name => child_model).tap do |ct|
ct.content_custom_fields.build :label => 'Body', :kind => 'string', :required => false
ct.content_custom_fields.build :label => parent_model.singularize, :kind => 'has_one', :required => false, :target => parent_model
ct.save!
end
@parent_model.content_custom_fields.build({
:label => child_model,
:kind => 'has_many',
:target => @child_model.content_klass.to_s,
:reverse_lookup => @child_model.content_klass.custom_field_alias_to_name(parent_model.downcase.singularize)
})
end
Then /^I should be able to view a paginaed list of a has many association$/ do
# Create models
Given %{I have an "Articles" model which has many "Comments"}
# Create contents
article = @parent_model.contents.create!(:slug => 'parent', :body => 'Parent')
@child_model.contents.create!(:slug => 'one', :body => 'One', :custom_field_2 => article.id.to_s)
@child_model.contents.create!(:slug => 'two', :body => 'Two', :custom_field_2 => article.id.to_s)
@child_model.contents.create!(:slug => 'three', :body => 'Three', :custom_field_2 => article.id.to_s)
@child_model.contents.each do |comment|
article.comments << comment
end
# Create a page
raw_template = %{
{% for article in contents.articles %}
{% paginate article.comments by 2 %}
{% for comment in paginate.collection %}
{{ comment.body }}
{% endfor %}
{{ paginate | default_pagination }}
{% endpaginate %}
{% endfor %}
}
# Create a page
FactoryGirl.create(:page, :site => @site, :slug => 'hello', :raw_template => raw_template)
# The page should have the first two comments
visit '/hello'
page.should have_content 'One'
page.should have_content 'Two'
page.should_not have_content 'Three'
# The second page should have the last comment
click_link '2'
page.should_not have_content 'One'
page.should_not have_content 'Two'
page.should have_content 'Three'
end

View File

@ -0,0 +1,35 @@
Then /^I should be able to display paginated models$/ do
# Create our article model and three articles
@article_model = FactoryGirl.build(:content_type, :site => @site, :name => 'Articles').tap do |ct|
ct.content_custom_fields.build :label => 'Body', :kind => 'string', :required => false
ct.save!
end
%w(First Second Third).each do |body|
@article_model.contents.create!(:body => body)
end
# Create a page with template
raw_template = %{
{% paginate contents.articles by 2 %}
{% for article in paginate.collection %}
{{ article.body }}
{% endfor %}
{{ paginate | default_pagination }}
{% endpaginate %}
}
FactoryGirl.create(:page, :site => @site, :slug => 'hello', :raw_template => raw_template)
# The page should have the first two articles
visit '/hello'
page.should have_content 'First'
page.should have_content 'Second'
page.should_not have_content 'Third'
# The second page should have the last article
click_link '2'
page.should_not have_content 'First'
page.should_not have_content 'Second'
page.should have_content 'Third'
end

View File

@ -13,6 +13,10 @@ Given /^I have the site: "([^"]*)" set up(?: with #{capture_fields})?$/ do |site
@admin.should_not be_nil @admin.should_not be_nil
end end
Given /^I have a site set up$/ do
Given %{I have the site: "test site" set up}
end
Given /^I have a designer and an author$/ do Given /^I have a designer and an author$/ do
FactoryGirl.create(:designer, :site => Site.first) FactoryGirl.create(:designer, :site => Site.first)
FactoryGirl.create(:author, :site => Site.first) FactoryGirl.create(:author, :site => Site.first)

View File

@ -9,6 +9,7 @@ require 'locomotive/logger'
require 'locomotive/formtastic' require 'locomotive/formtastic'
require 'locomotive/dragonfly' require 'locomotive/dragonfly'
require 'locomotive/kaminari'
require 'locomotive/liquid' require 'locomotive/liquid'
require 'locomotive/mongoid' require 'locomotive/mongoid'
require 'locomotive/carrierwave' require 'locomotive/carrierwave'

View File

@ -2,7 +2,7 @@ require 'devise'
require 'mongoid' require 'mongoid'
require 'mongoid/railtie' require 'mongoid/railtie'
require 'mongoid_acts_as_tree' require 'mongoid_acts_as_tree'
require 'will_paginate' require 'kaminari'
require 'haml' require 'haml'
require 'liquid' require 'liquid'
require 'formtastic' require 'formtastic'
@ -19,4 +19,4 @@ require 'dragonfly'
require 'cancan' require 'cancan'
require 'RMagick' require 'RMagick'
require 'cells' require 'cells'
require 'sanitize' require 'sanitize'

View File

@ -0,0 +1,17 @@
require 'kaminari'
module Kaminari
class PaginatableArray < Array
def to_liquid(options = {})
{
:collection => to_a,
:current_page => current_page,
:previous_page => first_page? ? nil : current_page - 1,
:next_page => last_page? ? nil : current_page + 1,
:total_entries => total_count,
:total_pages => num_pages,
:per_page => limit_value
}
end
end
end

View File

@ -64,16 +64,7 @@ module Locomotive
protected protected
def paginate(options = {}) def paginate(options = {})
@collection = self.collection.paginate(options) @collection = Kaminari.paginate_array(self.collection).page(options[:page]).per(options[:per_page])
{
:collection => @collection,
:current_page => @collection.current_page,
:previous_page => @collection.previous_page,
:next_page => @collection.next_page,
:total_entries => @collection.total_entries,
:total_pages => @collection.total_pages,
:per_page => @collection.per_page
}
end end
def collection def collection

View File

@ -36,13 +36,16 @@ module Locomotive
raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil? raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil?
pagination = collection.send(:paginate, { if collection.is_a? Array
:page => context['current_page'], pagination = Kaminari.paginate_array(collection).page(context['current_page']).per(@per_page).to_liquid.stringify_keys
:per_page => @per_page }).stringify_keys else
pagination = collection.send(:paginate, {
:page => context['current_page'],
:per_page => @per_page }).to_liquid.stringify_keys
end
page_count, current_page = pagination['total_pages'], pagination['current_page'] page_count, current_page = pagination['total_pages'], pagination['current_page']
path = context.registers[:page].fullpath path = context['path']
pagination['previous'] = link(I18n.t('pagination.previous'), current_page - 1, path) if pagination['previous_page'] pagination['previous'] = link(I18n.t('pagination.previous'), current_page - 1, path) if pagination['previous_page']
pagination['next'] = link(I18n.t('pagination.next'), current_page + 1, path) if pagination['next_page'] pagination['next'] = link(I18n.t('pagination.next'), current_page + 1, path) if pagination['next_page']

View File

@ -65,6 +65,7 @@ module Locomotive
'contents' => Locomotive::Liquid::Drops::Contents.new, 'contents' => Locomotive::Liquid::Drops::Contents.new,
'current_page' => self.params[:page], 'current_page' => self.params[:page],
'params' => self.params, 'params' => self.params,
'path' => request.path,
'url' => request.url, 'url' => request.url,
'now' => Time.now.utc, 'now' => Time.now.utc,
'today' => Date.today 'today' => Date.today

View File

@ -39,7 +39,7 @@ module Locomotive
end end
def render_no_site_error def render_no_site_error
render :template => '/admin/errors/no_site', :layout => false render :template => '/admin/errors/no_site', :layout => false, :status => :not_found
end end
def validate_site_membership def validate_site_membership

View File

@ -21,10 +21,14 @@ Gem::Specification.new do |s|
s.add_dependency 'warden' s.add_dependency 'warden'
s.add_dependency 'devise', '1.3.4' s.add_dependency 'devise', '1.3.4'
s.add_dependency 'devise_bushido_authenticatable', '1.0.0.alpha10' s.add_dependency 'devise_bushido_authenticatable', '1.0.0.alpha10'
s.add_dependency 'mongo', '~> 1.3.1'
s.add_dependency 'bson', '~> 1.3.1'
s.add_dependency 'bson_ext', '~> 1.3.1'
s.add_dependency 'mongoid', '~> 2.0.2' s.add_dependency 'mongoid', '~> 2.0.2'
s.add_dependency 'bson_ext', '~> 1.4.0'
s.add_dependency 'locomotive_mongoid_acts_as_tree', '0.1.5.7' s.add_dependency 'locomotive_mongoid_acts_as_tree', '0.1.5.7'
s.add_dependency 'will_paginate', '~> 3.0.0' s.add_dependency 'kaminari'
s.add_dependency 'haml', '3.1.2' s.add_dependency 'haml', '3.1.2'
s.add_dependency 'sass', '3.1.2' s.add_dependency 'sass', '3.1.2'

View File

@ -3,9 +3,6 @@ require 'spec_helper'
describe Locomotive::Liquid::Drops::Contents do describe Locomotive::Liquid::Drops::Contents do
before(:each) do before(:each) do
# Reload the file (needed for spork)
load File.join(Rails.root, 'lib', 'locomotive', 'liquid', 'drops', 'contents.rb')
@site = FactoryGirl.build(:site) @site = FactoryGirl.build(:site)
@content_type = FactoryGirl.build(:content_type, :site => @site, :slug => 'projects') @content_type = FactoryGirl.build(:content_type, :site => @site, :slug => 'projects')
end end

View File

@ -52,7 +52,8 @@ describe Locomotive::Liquid::Tags::Paginate do
{}, {},
{ {
'projects' => options.has_key?(:collection) ? options[:collection] : PaginatedCollection.new(['Ruby on Rails', 'jQuery', 'mongodb', 'Liquid', 'sqlite3']), 'projects' => options.has_key?(:collection) ? options[:collection] : PaginatedCollection.new(['Ruby on Rails', 'jQuery', 'mongodb', 'Liquid', 'sqlite3']),
'current_page' => options[:page] || 1 'current_page' => options[:page] || 1,
'path' => '/'
}, { }, {
:page => FactoryGirl.build(:page) :page => FactoryGirl.build(:page)
}, true) }, true)

View File

@ -147,11 +147,19 @@ describe Locomotive::Routing::SiteDispatcher do
describe '#render_no_site_error' do describe '#render_no_site_error' do
it 'renders the no site template with no layout' do before :each do
@controller.expects(:render).with(:template => '/admin/errors/no_site', :layout => false) @controller.instance_variable_set('@_response', ActionDispatch::Response.new)
@controller.send(:render_no_site_error) @controller.send(:render_no_site_error)
end end
it 'should have a no site error message' do
@controller.response.body.should include 'No Site'
end
it 'should have a 404 not found status' do
@controller.response.should be_not_found
end
end end
describe '#validate_site_membership' do describe '#validate_site_membership' do

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Ability do describe Locomotive::Ability do
before :each do before :each do
@site = FactoryGirl.create(:site) @site = FactoryGirl.create(:site)
@ -13,7 +13,7 @@ describe Ability do
context 'pages' do context 'pages' do
subject { Page.new } subject { Locomotive::Page.new }
context 'management' do context 'management' do
it 'should allow management of pages from (admin, designer, author)' do it 'should allow management of pages from (admin, designer, author)' do
@ -33,7 +33,7 @@ describe Ability do
context 'content instance' do context 'content instance' do
subject { ContentInstance.new } subject { Locomotive::ContentInstance.new }
context 'management' do context 'management' do
it 'should allow management of pages from (admin, designer, author)' do it 'should allow management of pages from (admin, designer, author)' do
@ -47,7 +47,7 @@ describe Ability do
context 'content type' do context 'content type' do
subject { ContentType.new } subject { Locomotive::ContentType.new }
context 'management' do context 'management' do
it 'should allow management of pages from (admin, designer)' do it 'should allow management of pages from (admin, designer)' do
@ -67,7 +67,7 @@ describe Ability do
context 'theme assets' do context 'theme assets' do
subject { ThemeAsset.new } subject { Locomotive::ThemeAsset.new }
context 'management' do context 'management' do
it 'should allow management of pages from (admin, designer)' do it 'should allow management of pages from (admin, designer)' do
@ -87,7 +87,7 @@ describe Ability do
context 'site' do context 'site' do
subject { Site.new } subject { Locomotive::Site.new }
context 'management' do context 'management' do
it 'should allow management of pages from (admin)' do it 'should allow management of pages from (admin)' do
@ -115,7 +115,7 @@ describe Ability do
context 'membership' do context 'membership' do
subject { Membership.new } subject { Locomotive::Membership.new }
context 'management' do context 'management' do
it 'should allow management of memberships from (admin, designer)' do it 'should allow management of memberships from (admin, designer)' do

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Account do describe Locomotive::Account do
it 'should have a valid factory' do it 'should have a valid factory' do
FactoryGirl.build(:account).should be_valid FactoryGirl.build(:account).should be_valid
@ -17,7 +17,7 @@ describe Account do
end end
it "should have a default locale" do it "should have a default locale" do
account = Account.new account = Locomotive::Account.new
account.locale.should == 'en' account.locale.should == 'en'
end end
@ -31,8 +31,8 @@ describe 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 => [Membership.new(:account => account)]) site_1 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
site_2 = FactoryGirl.create(:site, :subdomain => 'foo', :memberships => [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] account.reload.sites.to_a.should == [site_1, site_2]
end end
@ -40,14 +40,14 @@ describe Account do
before(:each) do before(:each) do
@account = FactoryGirl.build(:account) @account = FactoryGirl.build(:account)
@site_1 = FactoryGirl.build(:site, :subdomain => 'foo', :memberships => [FactoryGirl.build(:membership, :account => @account)]) @site_1 = FactoryGirl.build(:site,:memberships => [FactoryGirl.build(:membership, :account => @account)])
@site_2 = FactoryGirl.build(:site, :subdomain => 'bar', :memberships => [FactoryGirl.build(:membership, :account => @account)]) @site_2 = FactoryGirl.build(:site,:memberships => [FactoryGirl.build(:membership, :account => @account)])
@account.stubs(:sites).returns([@site_1, @site_2]) @account.stubs(:sites).returns([@site_1, @site_2])
Site.any_instance.stubs(:save).returns(true) Locomotive::Site.any_instance.stubs(:save).returns(true)
end end
it 'should also delete memberships' do it 'should also delete memberships' do
Site.any_instance.stubs(:admin_memberships).returns(['junk', 'dirt']) Locomotive::Site.any_instance.stubs(:admin_memberships).returns(['junk', 'dirt'])
@site_1.memberships.first.expects(:destroy) @site_1.memberships.first.expects(:destroy)
@site_2.memberships.first.expects(:destroy) @site_2.memberships.first.expects(:destroy)
@account.destroy @account.destroy
@ -78,13 +78,13 @@ describe Account do
context 'retrieving an account' do context 'retrieving an account' do
it 'does not find it with an empty token' do it 'does not find it with an empty token' do
Account.find_using_switch_site_token(nil).should be_nil Locomotive::Account.find_using_switch_site_token(nil).should be_nil
end end
it 'raises an exception if not found' do it 'raises an exception if not found' do
lambda { expect {
Account.find_using_switch_site_token!(nil) Locomotive::Account.find_using_switch_site_token!(nil)
}.should raise_error(Mongoid::Errors::DocumentNotFound) }.to raise_error Mongoid::Errors::DocumentNotFound
end end
end end

View File

@ -2,12 +2,12 @@
require 'spec_helper' require 'spec_helper'
describe Asset do describe Locomotive::ContentAsset do
describe 'attaching a file' do describe 'attaching a file' do
before(:each) do before(:each) do
Asset.any_instance.stubs(:site_id).returns('test') Locomotive::ContentAsset.any_instance.stubs(:site_id).returns('test')
@asset = FactoryGirl.build(:asset) @asset = FactoryGirl.build(:asset)
end end
@ -43,4 +43,4 @@ describe Asset do
end end
end end

View File

@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe ContentInstance do describe Locomotive::ContentInstance do
before(:each) do before(:each) do
Site.any_instance.stubs(:create_default_pages!).returns(true) Site.any_instance.stubs(:create_default_pages!).returns(true)
@ -189,4 +189,4 @@ describe ContentInstance do
def fake_bson_id(id) def fake_bson_id(id)
BSON::ObjectId(id.to_s.rjust(24, '0')) BSON::ObjectId(id.to_s.rjust(24, '0'))
end end
end end

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe ContentType do describe Locomotive::ContentType do
before(:each) do before(:each) do
Site.any_instance.stubs(:create_default_pages!).returns(true) Site.any_instance.stubs(:create_default_pages!).returns(true)

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe EditableElement do describe Locomotive::EditableElement do
before(:each) do before(:each) do
@site = FactoryGirl.create(:site) @site = FactoryGirl.create(:site)
@ -52,4 +52,4 @@ describe EditableElement do
end end
end end
end end

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Membership do describe Locomotive::Membership do
it 'should have a valid factory' do it 'should have a valid factory' do
FactoryGirl.build(:membership, :account => FactoryGirl.build(:account)).should be_valid FactoryGirl.build(:membership, :account => FactoryGirl.build(:account)).should be_valid

View File

@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Page do describe Locomotive::Page do
before(:each) do before(:each) do
Site.any_instance.stubs(:create_default_pages!).returns(true) Site.any_instance.stubs(:create_default_pages!).returns(true)

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Site do describe Locomotive::Site do
it 'should have a valid factory' do it 'should have a valid factory' do
FactoryGirl.build(:site).should be_valid FactoryGirl.build(:site).should be_valid

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Snippet do describe Locomotive::Snippet do
it 'should have a valid factory' do it 'should have a valid factory' do
FactoryGirl.build(:snippet).should be_valid FactoryGirl.build(:snippet).should be_valid

View File

@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe ThemeAsset do describe Locomotive::ThemeAsset do
describe 'attaching a file' do describe 'attaching a file' do

View File

@ -1,93 +1,41 @@
# Note: if segmentation fault with spork / imagemagick on mac os x, take a look at: ENV["RAILS_ENV"] ||= 'test'
# http://stackoverflow.com/questions/2838307/why-is-this-rmagick-call-generating-a-segmentation-fault
require 'rubygems' require 'rails/mongoid'
require 'spork' require File.join(File.dirname(__FILE__), 'dummy', 'config', 'environment.rb')
require 'rspec/rails'
require 'factory_girl'
# figure out where we are being loaded from # Requires supporting ruby files with custom matchers and macros, etc,
if $LOADED_FEATURES.grep(/spec\/spec_helper\.rb/).any? # in spec/support/ and its subdirectories.
begin Dir[File.join(File.dirname(__FILE__), 'support', '**', '*.rb')].each { |f| require f }
raise "foo"
rescue => e
puts <<-MSG
===================================================
It looks like spec_helper.rb has been loaded
multiple times. Normalize the require to:
require "spec/spec_helper" Locomotive.configure_for_test
Things like File.join and File.expand_path will RSpec.configure do |config|
cause it to be loaded multiple times.
Loaded this time from: config.include(Locomotive::RSpec::Matchers)
#{e.backtrace.join("\n ")} config.mock_with :mocha
===================================================
MSG config.before(:each) do
Locomotive.config.heroku = false
end end
end
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = 'mongoid'
end
Spork.prefork do config.before(:each) do
# Loading more in this block will cause your tests to run faster. However, if self.described_class != Locomotive::Import::Job
# if you change any configuration or code from libraries loaded here, you'll DatabaseCleaner.clean
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
# Avoid preloading models
require 'rails/mongoid'
Spork.trap_class_method(Rails::Mongoid, :load_models)
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'factory_girl'
Spork.trap_class_method(FactoryGirl, :find_definitions)
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
Locomotive.configure_for_test
RSpec.configure do |config|
config.include(Locomotive::RSpec::Matchers)
config.mock_with :mocha
config.before(:each) do
Locomotive.config.heroku = false
end
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = 'mongoid'
end
config.before(:each) do
if self.described_class != Locomotive::Import::Job
DatabaseCleaner.clean
end
end
config.before(:all) do
if self.described_class == Locomotive::Import::Job
DatabaseCleaner.clean
end
end end
end end
config.before(:all) do
if self.described_class == Locomotive::Import::Job
DatabaseCleaner.clean
end
end
end end
Spork.each_run do
# This code will be run each time you run your specs.
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# loading ruby file directly breaks the tests
# Dir[Rails.root.join('app/models/*.rb')].each { |f| load f }
end

View File

@ -1,22 +1,22 @@
FactoryGirl.define do FactoryGirl.define do
## Site ## ## Site ##
factory :site do factory :site, :class => Locomotive::Site do
name 'Acme Website' name 'Acme Website'
subdomain 'acme' # subdomain 'acme'
created_at Time.now created_at Time.now
factory "test site" do factory "test site" do
name 'Locomotive test website' name 'Locomotive test website'
subdomain 'test' # subdomain 'test'
after_build do |site_test| after_build do |site_test|
site_test.memberships.build :account => Account.where(:name => "Admin").first || Factory("admin user"), :role => 'admin' site_test.memberships.build :account => Locomotive::Account.where(:name => "Admin").first || Factory("admin user"), :role => 'admin'
end end
factory "another site" do factory "another site" do
name "Locomotive test website #2" name "Locomotive test website #2"
subdomain "test2" # subdomain "test2"
end end
end end
@ -41,7 +41,7 @@ FactoryGirl.define do
end end
# Accounts ## # Accounts ##
factory :account do factory :account, :class => Locomotive::Account do
name 'Bart Simpson' name 'Bart Simpson'
email 'bart@simpson.net' email 'bart@simpson.net'
password 'easyone' password 'easyone'
@ -74,7 +74,7 @@ FactoryGirl.define do
end end
## Memberships ## ## Memberships ##
factory :membership do factory :membership, :class => Locomotive::Membership do
role 'admin' role 'admin'
account { Account.where(:name => "Bart Simpson").first || Factory('admin user') } account { Account.where(:name => "Bart Simpson").first || Factory('admin user') }
@ -96,46 +96,49 @@ FactoryGirl.define do
end end
## Pages ## ## Pages ##
factory :page do factory :page, :class => Locomotive::Page do
title 'Home page' title 'Home page'
slug 'index' slug 'index'
published true published true
site { Site.where(:subdomain => "acme").first || Factory(:site) } site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) }
factory :sub_page do factory :sub_page do
title 'Subpage' title 'Subpage'
slug 'subpage' slug 'subpage'
published true published true
site { Site.where(:subdomain => "acme").first || Factory(:site) } site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) }
parent { Page.where(:slug => "index").first || Factory(:page) } parent { Locomotive::Page.where(:slug => "index").first || Factory(:page) }
end end
end end
## Snippets ## ## Snippets ##
factory :snippet do factory :snippet, :class => Locomotive::Snippet do
name 'My website title' name 'My website title'
slug 'header' slug 'header'
template %{<title>Acme</title>} template %{<title>Acme</title>}
site { Site.where(:subdomain => "acme").first || Factory(:site) } site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) }
end end
## Assets ## ## Assets ##
factory :asset do factory :asset, :class => Locomotive::ContentAsset do
site { Site.where(:subdomain => "acme").first || Factory(:site) } site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) }
end end
## Theme assets ## ## Theme assets ##
factory :theme_asset do factory :theme_asset, :class => Locomotive::ThemeAsset do
site { Site.where(:subdomain => "acme").first || Factory(:site) } site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) }
end end
## Content types ## ## Content types ##
factory :content_type do factory :content_type, :class => Locomotive::ContentType do
name 'My project' name 'My project'
site { Site.where(:subdomain => "acme").first || Factory(:site) } site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) }
end end
end factory :content_instance, :class => Locomotive::ContentInstance do
end
end

View File

@ -29,11 +29,11 @@ def Locomotive.configure_for_test(force = false)
Locomotive.define_subdomain_and_domains_options Locomotive.define_subdomain_and_domains_options
Object.send(:remove_const, 'Site') if Object.const_defined?('Site') Object.send(:remove_const, 'Locomotive::Site') if Object.const_defined?('Locomotive::Site')
load 'site.rb' load 'locomotive/site.rb'
FactoryGirl.factories.clear FactoryGirl.factories.clear
load File.join(Rails.root, 'spec', 'factories.rb') load File.join(Rails.root, 'spec', 'factories.rb')
end end
end end
end end