diff --git a/Gemfile b/Gemfile index efbb8757..35e28a75 100644 --- a/Gemfile +++ b/Gemfile @@ -6,22 +6,18 @@ gem 'rake', '0.9.2' gem 'rails', '~> 3.1.1' -gem 'devise', :git => 'git://github.com/plataformatec/devise.git' -# gem 'devise', '~> 1.4.9' # FIXME: waiting for the new devise gem coming soon +gem 'devise', :git => 'git://github.com/plataformatec/devise.git'# FIXME: waiting for the new devise gem coming soon gem 'cancan', '~> 1.6.7' -gem 'bson', '~> 1.3.1' -gem 'mongo', '~> 1.3.1' -gem 'bson_ext', '~> 1.3.1' +gem 'bson_ext', '~> 1.4.0' 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', '~> 1.1.0.rc1', :path => '../gems/custom_fields' -gem 'will_paginate', '~> 3.0.2' +gem 'kaminari' gem 'haml', '~> 3.1.3' 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 'uglifier', '~> 1.0.4' gem 'jquery-rails', '~> 1.0.16' @@ -82,7 +78,6 @@ group :test do gem 'capybara' gem 'database_cleaner' - gem 'spork', '~> 0.9.0.rc' gem 'launchy' gem 'mocha', '0.9.12' # :git => 'git://github.com/floehopper/mocha.git' end diff --git a/Gemfile.lock b/Gemfile.lock index a4d8360f..3c9544ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,8 @@ GIT remote: git://github.com/chriseppstein/compass.git - revision: 22e2458b77519e8eb8463170c1a1fe4bab105f3e - branch: rails31 + revision: ebd758a7999a74611a3d682ba72c661a83f97498 specs: - compass (0.12.0.alpha.0.22e2458) + compass (0.12.alpha.0.ebd758a) chunky_png (~> 1.2) fssm (>= 0.2.7) sass (~> 3.1) @@ -27,7 +26,7 @@ GIT GIT remote: git://github.com/plataformatec/devise.git - revision: e8aabd465216bcb260448056c8ed4580d099a8c9 + revision: a5aa03b98328d8e1fdb4081a74b3c04ff9593607 specs: devise (1.5.0.dev) bcrypt-ruby (~> 3.0) @@ -40,8 +39,9 @@ GIT specs: handlebars-rails (0.9.1) -PATH - remote: ../gems/acts_as_tree +GIT + remote: git@github.com:locomotivecms/mongoid_acts_as_tree.git + revision: ca494d22c3d7946385aba1153c017d9c30e9f9d3 specs: locomotive_mongoid_acts_as_tree (0.1.5.7) @@ -87,8 +87,8 @@ GEM autotest (4.4.6) ZenTest (>= 4.4.1) bcrypt-ruby (3.0.1) - bson (1.3.1) - bson_ext (1.3.1) + bson (1.4.0) + bson_ext (1.4.0) builder (3.0.0) bushido (0.0.35) highline (>= 1.6.1) @@ -103,7 +103,7 @@ GEM rack-test (>= 0.5.4) selenium-webdriver (~> 2.0) xpath (~> 0.1.4) - carrierwave (0.5.7) + carrierwave (0.5.8) activesupport (~> 3.0) carrierwave-mongoid (0.1.3) carrierwave (>= 0.5.6) @@ -120,10 +120,10 @@ GEM execjs coffee-script-source (1.1.3) columnize (0.3.4) - cucumber (1.1.2) + cucumber (1.1.1) builder (>= 2.1.2) diff-lcs (>= 1.1.2) - gherkin (~> 2.6.2) + gherkin (~> 2.6.0) json (>= 1.4.6) term-ansicolor (>= 1.0.6) cucumber-rails (1.2.0) @@ -178,11 +178,13 @@ GEM multi_json multi_xml i18n (0.6.0) - jquery-rails (1.0.17) + jquery-rails (1.0.16) railties (~> 3.0) thor (~> 0.14) - json (1.5.4) + json (1.6.1) json_pure (1.6.1) + kaminari (0.12.4) + rails (>= 3.0.0) kgio (2.6.0) launchy (2.0.5) addressable (~> 2.2.6) @@ -197,8 +199,8 @@ GEM mime-types (1.17.2) mimetype-fu (0.1.2) mocha (0.9.12) - mongo (1.3.1) - bson (>= 1.3.1) + mongo (1.4.0) + bson (= 1.4.0) mongoid (2.3.3) activemodel (~> 3.1) mongo (~> 1.3) @@ -284,7 +286,7 @@ GEM archive-tar-minitar (>= 0.5.2) rubyzip (0.9.4) sanitize (2.0.3) - nokogiri (< 1.6, >= 1.4.4) + nokogiri (>= 1.4.4, < 1.6) sass (3.1.10) sass-rails (3.1.4) actionpack (~> 3.1.0) @@ -297,11 +299,10 @@ GEM ffi (= 1.0.9) json_pure rubyzip - spork (0.9.0.rc9) sprockets (2.0.3) hike (~> 1.2) rack (~> 1.0) - tilt (!= 1.3.0, ~> 1.1) + tilt (~> 1.1, != 1.3.0) term-ansicolor (1.0.7) thor (0.14.6) tilt (1.3.3) @@ -320,7 +321,6 @@ GEM raindrops (~> 0.6) warden (1.1.0) rack (>= 1.0) - will_paginate (3.0.2) xpath (0.1.4) nokogiri (~> 1.3) @@ -333,8 +333,7 @@ DEPENDENCIES ZenTest actionmailer-with-request (~> 0.3.0) autotest - bson (~> 1.3.1) - bson_ext (~> 1.3.1) + bson_ext (~> 1.4.0) bushido (= 0.0.35) cancan (~> 1.6.7) capybara @@ -360,13 +359,13 @@ DEPENDENCIES highline (~> 1.6.2) httparty (~> 0.8.1) jquery-rails (~> 1.0.16) + kaminari launchy linecache (= 0.43) locomotive_liquid (= 2.2.2) - locomotive_mongoid_acts_as_tree (= 0.1.5.7)! + locomotive_mongoid_acts_as_tree! mimetype-fu (~> 0.1.2) mocha (= 0.9.12) - mongo (~> 1.3.1) mongoid (~> 2.3.3) pickle rack-cache (~> 1.1) @@ -382,10 +381,8 @@ DEPENDENCIES rubyzip sanitize (~> 2.0.3) sass-rails (~> 3.1.4) - spork (~> 0.9.0.rc) tinymce-rails uglifier (~> 1.0.4) unicorn uploadify-rails! - will_paginate (~> 3.0.2) xpath (~> 0.1.4) diff --git a/app/controllers/locomotive/import_controller.rb b/app/controllers/locomotive/import_controller.rb index 2492bb71..c5db218a 100644 --- a/app/controllers/locomotive/import_controller.rb +++ b/app/controllers/locomotive/import_controller.rb @@ -3,8 +3,6 @@ module Locomotive sections 'settings', 'site' - actions :show, :new, :create - skip_load_and_authorize_resource before_filter :authorize_import @@ -23,7 +21,8 @@ module Locomotive end end - def new; end + def new + end def create begin @@ -52,4 +51,4 @@ module Locomotive end end -end \ No newline at end of file +end diff --git a/app/controllers/locomotive/my_account_controller.rb b/app/controllers/locomotive/my_account_controller.rb index d9d659ec..95f33986 100644 --- a/app/controllers/locomotive/my_account_controller.rb +++ b/app/controllers/locomotive/my_account_controller.rb @@ -3,12 +3,13 @@ module Locomotive sections 'settings', 'account' - actions :edit, :update - respond_to :json, :only => :update skip_load_and_authorize_resource + def edit + end + def update update! { edit_my_account_url } end diff --git a/app/controllers/locomotive/sites_controller.rb b/app/controllers/locomotive/sites_controller.rb index feaed930..5f031e0e 100644 --- a/app/controllers/locomotive/sites_controller.rb +++ b/app/controllers/locomotive/sites_controller.rb @@ -1,8 +1,6 @@ module Locomotive class SitesController < BaseController - defaults :instance_name => 'site' - sections 'settings' def create diff --git a/app/models/locomotive/ability.rb b/app/models/locomotive/ability.rb index eeb46f02..54c7bc19 100644 --- a/app/models/locomotive/ability.rb +++ b/app/models/locomotive/ability.rb @@ -32,7 +32,7 @@ module Locomotive can :touch, [Page, ThemeAsset] can :sort, Page - can :manage, [ContentInstance, Asset] + can :manage, [ContentInstance, ContentAsset] can :touch, Site do |site| site == @site @@ -50,7 +50,7 @@ module Locomotive can :manage, ThemeAsset - can :manage, Asset + can :manage, ContentAsset can :manage, Site do |site| site == @site @@ -82,4 +82,4 @@ module Locomotive end end end -end \ No newline at end of file +end diff --git a/app/models/locomotive/account.rb b/app/models/locomotive/account.rb index 0bd6a1df..9a47801c 100644 --- a/app/models/locomotive/account.rb +++ b/app/models/locomotive/account.rb @@ -27,7 +27,7 @@ module Locomotive end 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 end @@ -37,7 +37,7 @@ module Locomotive end 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 def devise_mailer @@ -67,4 +67,4 @@ module Locomotive end end -end \ No newline at end of file +end diff --git a/app/models/locomotive/content_instance.rb b/app/models/locomotive/content_instance.rb index 36f54f23..5ce46de8 100644 --- a/app/models/locomotive/content_instance.rb +++ b/app/models/locomotive/content_instance.rb @@ -5,7 +5,11 @@ module Locomotive include ::Mongoid::Timestamps ## 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 ## fields (dynamic fields) ## @@ -124,4 +128,4 @@ module Locomotive end end -end \ No newline at end of file +end diff --git a/app/models/locomotive/content_type.rb b/app/models/locomotive/content_type.rb index 219c78b5..3c283800 100644 --- a/app/models/locomotive/content_type.rb +++ b/app/models/locomotive/content_type.rb @@ -144,4 +144,4 @@ module Locomotive end end -end \ No newline at end of file +end diff --git a/config/initializers/will_paginate.rb b/config/initializers/will_paginate.rb deleted file mode 100644 index e78a36c5..00000000 --- a/config/initializers/will_paginate.rb +++ /dev/null @@ -1 +0,0 @@ -# require 'will_paginate/array' diff --git a/features/engine/has_many.feature b/features/engine/has_many.feature new file mode 100644 index 00000000..3d07ea13 --- /dev/null +++ b/features/engine/has_many.feature @@ -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 diff --git a/features/engine/pagination.feature b/features/engine/pagination.feature new file mode 100644 index 00000000..6fa8234c --- /dev/null +++ b/features/engine/pagination.feature @@ -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 diff --git a/features/step_definitions/content_types_steps.rb b/features/step_definitions/content_types_steps.rb index 6e65375b..d51255c4 100644 --- a/features/step_definitions/content_types_steps.rb +++ b/features/step_definitions/content_types_steps.rb @@ -66,4 +66,3 @@ end Then %r{^I should see once the "([^"]*)" field$} do |field| page.should have_css("#content_#{field.underscore.downcase}_input", :count => 1) end - diff --git a/features/step_definitions/has_many_steps.rb b/features/step_definitions/has_many_steps.rb new file mode 100644 index 00000000..4d4e5ce0 --- /dev/null +++ b/features/step_definitions/has_many_steps.rb @@ -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 diff --git a/features/step_definitions/pagination_steps.rb b/features/step_definitions/pagination_steps.rb new file mode 100644 index 00000000..b0d53167 --- /dev/null +++ b/features/step_definitions/pagination_steps.rb @@ -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 + diff --git a/features/step_definitions/site_steps.rb b/features/step_definitions/site_steps.rb index 075a451d..ee03442a 100644 --- a/features/step_definitions/site_steps.rb +++ b/features/step_definitions/site_steps.rb @@ -13,6 +13,10 @@ Given /^I have the site: "([^"]*)" set up(?: with #{capture_fields})?$/ do |site @admin.should_not be_nil 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 FactoryGirl.create(:designer, :site => Site.first) FactoryGirl.create(:author, :site => Site.first) diff --git a/lib/locomotive.rb b/lib/locomotive.rb index dc65c01d..e0e6c3ac 100644 --- a/lib/locomotive.rb +++ b/lib/locomotive.rb @@ -9,6 +9,7 @@ require 'locomotive/logger' require 'locomotive/formtastic' require 'locomotive/dragonfly' +require 'locomotive/kaminari' require 'locomotive/liquid' require 'locomotive/mongoid' require 'locomotive/carrierwave' diff --git a/lib/locomotive/dependencies.rb b/lib/locomotive/dependencies.rb index 4397e7d4..23904ab2 100644 --- a/lib/locomotive/dependencies.rb +++ b/lib/locomotive/dependencies.rb @@ -2,7 +2,7 @@ require 'devise' require 'mongoid' require 'mongoid/railtie' require 'mongoid_acts_as_tree' -require 'will_paginate' +require 'kaminari' require 'haml' require 'liquid' require 'formtastic' @@ -19,4 +19,4 @@ require 'dragonfly' require 'cancan' require 'RMagick' require 'cells' -require 'sanitize' \ No newline at end of file +require 'sanitize' diff --git a/lib/locomotive/kaminari.rb b/lib/locomotive/kaminari.rb new file mode 100644 index 00000000..d54fa7da --- /dev/null +++ b/lib/locomotive/kaminari.rb @@ -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 diff --git a/lib/locomotive/liquid/drops/contents.rb b/lib/locomotive/liquid/drops/contents.rb index 2f590ce1..d9bcb037 100644 --- a/lib/locomotive/liquid/drops/contents.rb +++ b/lib/locomotive/liquid/drops/contents.rb @@ -64,16 +64,7 @@ module Locomotive protected def paginate(options = {}) - @collection = self.collection.paginate(options) - { - :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 - } + @collection = Kaminari.paginate_array(self.collection).page(options[:page]).per(options[:per_page]) end def collection diff --git a/lib/locomotive/liquid/tags/paginate.rb b/lib/locomotive/liquid/tags/paginate.rb index eb41575a..93f71fa3 100644 --- a/lib/locomotive/liquid/tags/paginate.rb +++ b/lib/locomotive/liquid/tags/paginate.rb @@ -36,13 +36,16 @@ module Locomotive raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil? - pagination = collection.send(:paginate, { - :page => context['current_page'], - :per_page => @per_page }).stringify_keys - + if collection.is_a? Array + pagination = Kaminari.paginate_array(collection).page(context['current_page']).per(@per_page).to_liquid.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'] - path = context.registers[:page].fullpath + path = context['path'] 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'] diff --git a/lib/locomotive/render.rb b/lib/locomotive/render.rb index 5b8a14af..a097bb23 100644 --- a/lib/locomotive/render.rb +++ b/lib/locomotive/render.rb @@ -65,6 +65,7 @@ module Locomotive 'contents' => Locomotive::Liquid::Drops::Contents.new, 'current_page' => self.params[:page], 'params' => self.params, + 'path' => request.path, 'url' => request.url, 'now' => Time.now.utc, 'today' => Date.today diff --git a/lib/locomotive/routing/site_dispatcher.rb b/lib/locomotive/routing/site_dispatcher.rb index 0fe9648a..d8494cbb 100644 --- a/lib/locomotive/routing/site_dispatcher.rb +++ b/lib/locomotive/routing/site_dispatcher.rb @@ -39,7 +39,7 @@ module Locomotive end 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 def validate_site_membership diff --git a/locomotive_cms.gemspec b/locomotive_cms.gemspec index 4cde6e19..aed7e666 100644 --- a/locomotive_cms.gemspec +++ b/locomotive_cms.gemspec @@ -21,10 +21,14 @@ Gem::Specification.new do |s| s.add_dependency 'warden' s.add_dependency 'devise', '1.3.4' 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 'bson_ext', '~> 1.4.0' + 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 'sass', '3.1.2' diff --git a/spec/lib/locomotive/liquid/drops/contents_spec.rb b/spec/lib/locomotive/liquid/drops/contents_spec.rb index a0bb806d..d39758b1 100644 --- a/spec/lib/locomotive/liquid/drops/contents_spec.rb +++ b/spec/lib/locomotive/liquid/drops/contents_spec.rb @@ -3,9 +3,6 @@ require 'spec_helper' describe Locomotive::Liquid::Drops::Contents 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) @content_type = FactoryGirl.build(:content_type, :site => @site, :slug => 'projects') end diff --git a/spec/lib/locomotive/liquid/tags/paginate_spec.rb b/spec/lib/locomotive/liquid/tags/paginate_spec.rb index 4f63f707..2058320a 100644 --- a/spec/lib/locomotive/liquid/tags/paginate_spec.rb +++ b/spec/lib/locomotive/liquid/tags/paginate_spec.rb @@ -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']), - 'current_page' => options[:page] || 1 + 'current_page' => options[:page] || 1, + 'path' => '/' }, { :page => FactoryGirl.build(:page) }, true) diff --git a/spec/lib/locomotive/routing/site_dispatcher_spec.rb b/spec/lib/locomotive/routing/site_dispatcher_spec.rb index 731e8910..360a21ae 100644 --- a/spec/lib/locomotive/routing/site_dispatcher_spec.rb +++ b/spec/lib/locomotive/routing/site_dispatcher_spec.rb @@ -147,11 +147,19 @@ describe Locomotive::Routing::SiteDispatcher do describe '#render_no_site_error' do - it 'renders the no site template with no layout' do - @controller.expects(:render).with(:template => '/admin/errors/no_site', :layout => false) + before :each do + @controller.instance_variable_set('@_response', ActionDispatch::Response.new) @controller.send(:render_no_site_error) 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 describe '#validate_site_membership' do diff --git a/spec/models/ability_spec.rb b/spec/models/locomotive/ability_spec.rb similarity index 92% rename from spec/models/ability_spec.rb rename to spec/models/locomotive/ability_spec.rb index 237426ae..5d0efc13 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/locomotive/ability_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Ability do +describe Locomotive::Ability do before :each do @site = FactoryGirl.create(:site) @@ -13,7 +13,7 @@ describe Ability do context 'pages' do - subject { Page.new } + subject { Locomotive::Page.new } context 'management' do it 'should allow management of pages from (admin, designer, author)' do @@ -33,7 +33,7 @@ describe Ability do context 'content instance' do - subject { ContentInstance.new } + subject { Locomotive::ContentInstance.new } context 'management' do it 'should allow management of pages from (admin, designer, author)' do @@ -47,7 +47,7 @@ describe Ability do context 'content type' do - subject { ContentType.new } + subject { Locomotive::ContentType.new } context 'management' do it 'should allow management of pages from (admin, designer)' do @@ -67,7 +67,7 @@ describe Ability do context 'theme assets' do - subject { ThemeAsset.new } + subject { Locomotive::ThemeAsset.new } context 'management' do it 'should allow management of pages from (admin, designer)' do @@ -87,7 +87,7 @@ describe Ability do context 'site' do - subject { Site.new } + subject { Locomotive::Site.new } context 'management' do it 'should allow management of pages from (admin)' do @@ -115,7 +115,7 @@ describe Ability do context 'membership' do - subject { Membership.new } + subject { Locomotive::Membership.new } context 'management' do it 'should allow management of memberships from (admin, designer)' do diff --git a/spec/models/account_spec.rb b/spec/models/locomotive/account_spec.rb similarity index 70% rename from spec/models/account_spec.rb rename to spec/models/locomotive/account_spec.rb index c8f429a8..d866bce3 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/locomotive/account_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Account do +describe Locomotive::Account do it 'should have a valid factory' do FactoryGirl.build(:account).should be_valid @@ -17,7 +17,7 @@ describe Account do end it "should have a default locale" do - account = Account.new + account = Locomotive::Account.new account.locale.should == 'en' end @@ -31,8 +31,8 @@ describe Account do it 'should own many sites' do account = FactoryGirl.create(:account) - site_1 = FactoryGirl.create(:site, :memberships => [Membership.new(:account => account)]) - site_2 = FactoryGirl.create(:site, :subdomain => 'foo', :memberships => [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)]) account.reload.sites.to_a.should == [site_1, site_2] end @@ -40,14 +40,14 @@ describe Account do before(:each) do @account = FactoryGirl.build(:account) - @site_1 = FactoryGirl.build(:site, :subdomain => 'foo', :memberships => [FactoryGirl.build(:membership, :account => @account)]) - @site_2 = FactoryGirl.build(:site, :subdomain => 'bar', :memberships => [FactoryGirl.build(:membership, :account => @account)]) + @site_1 = FactoryGirl.build(:site,: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]) - Site.any_instance.stubs(:save).returns(true) + Locomotive::Site.any_instance.stubs(:save).returns(true) end 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_2.memberships.first.expects(:destroy) @account.destroy @@ -78,13 +78,13 @@ describe Account do context 'retrieving an account' 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 it 'raises an exception if not found' do - lambda { - Account.find_using_switch_site_token!(nil) - }.should raise_error(Mongoid::Errors::DocumentNotFound) + expect { + Locomotive::Account.find_using_switch_site_token!(nil) + }.to raise_error Mongoid::Errors::DocumentNotFound end end diff --git a/spec/models/asset_spec.rb b/spec/models/locomotive/content_asset_spec.rb similarity index 89% rename from spec/models/asset_spec.rb rename to spec/models/locomotive/content_asset_spec.rb index 58b4afc6..6599af8c 100644 --- a/spec/models/asset_spec.rb +++ b/spec/models/locomotive/content_asset_spec.rb @@ -2,12 +2,12 @@ require 'spec_helper' -describe Asset do +describe Locomotive::ContentAsset do describe 'attaching a file' 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) end @@ -43,4 +43,4 @@ describe Asset do end -end \ No newline at end of file +end diff --git a/spec/models/content_instance_spec.rb b/spec/models/locomotive/content_instance_spec.rb similarity index 99% rename from spec/models/content_instance_spec.rb rename to spec/models/locomotive/content_instance_spec.rb index 9669ab61..3f35d9a4 100644 --- a/spec/models/content_instance_spec.rb +++ b/spec/models/locomotive/content_instance_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ContentInstance do +describe Locomotive::ContentInstance do before(:each) do Site.any_instance.stubs(:create_default_pages!).returns(true) @@ -189,4 +189,4 @@ describe ContentInstance do def fake_bson_id(id) BSON::ObjectId(id.to_s.rjust(24, '0')) end -end \ No newline at end of file +end diff --git a/spec/models/content_type_spec.rb b/spec/models/locomotive/content_type_spec.rb similarity index 99% rename from spec/models/content_type_spec.rb rename to spec/models/locomotive/content_type_spec.rb index 73f72679..8ce132e1 100644 --- a/spec/models/content_type_spec.rb +++ b/spec/models/locomotive/content_type_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ContentType do +describe Locomotive::ContentType do before(:each) do Site.any_instance.stubs(:create_default_pages!).returns(true) diff --git a/spec/models/editable_element_spec.rb b/spec/models/locomotive/editable_element_spec.rb similarity index 97% rename from spec/models/editable_element_spec.rb rename to spec/models/locomotive/editable_element_spec.rb index 79901bae..ec2f33c3 100644 --- a/spec/models/editable_element_spec.rb +++ b/spec/models/locomotive/editable_element_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe EditableElement do +describe Locomotive::EditableElement do before(:each) do @site = FactoryGirl.create(:site) @@ -52,4 +52,4 @@ describe EditableElement do end end -end \ No newline at end of file +end diff --git a/spec/models/membership_spec.rb b/spec/models/locomotive/membership_spec.rb similarity index 98% rename from spec/models/membership_spec.rb rename to spec/models/locomotive/membership_spec.rb index f2c5fffe..62a0d64b 100644 --- a/spec/models/membership_spec.rb +++ b/spec/models/locomotive/membership_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Membership do +describe Locomotive::Membership do it 'should have a valid factory' do FactoryGirl.build(:membership, :account => FactoryGirl.build(:account)).should be_valid diff --git a/spec/models/page_spec.rb b/spec/models/locomotive/page_spec.rb similarity index 99% rename from spec/models/page_spec.rb rename to spec/models/locomotive/page_spec.rb index 4e2fcaca..409c9522 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/locomotive/page_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Page do +describe Locomotive::Page do before(:each) do Site.any_instance.stubs(:create_default_pages!).returns(true) diff --git a/spec/models/site_spec.rb b/spec/models/locomotive/site_spec.rb similarity index 99% rename from spec/models/site_spec.rb rename to spec/models/locomotive/site_spec.rb index fa434f73..21a069d2 100644 --- a/spec/models/site_spec.rb +++ b/spec/models/locomotive/site_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Site do +describe Locomotive::Site do it 'should have a valid factory' do FactoryGirl.build(:site).should be_valid diff --git a/spec/models/snippet_spec.rb b/spec/models/locomotive/snippet_spec.rb similarity index 97% rename from spec/models/snippet_spec.rb rename to spec/models/locomotive/snippet_spec.rb index 8f4a6d7e..a09d62da 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/locomotive/snippet_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Snippet do +describe Locomotive::Snippet do it 'should have a valid factory' do FactoryGirl.build(:snippet).should be_valid diff --git a/spec/models/theme_asset_spec.rb b/spec/models/locomotive/theme_asset_spec.rb similarity index 98% rename from spec/models/theme_asset_spec.rb rename to spec/models/locomotive/theme_asset_spec.rb index 16abe137..e485c529 100644 --- a/spec/models/theme_asset_spec.rb +++ b/spec/models/locomotive/theme_asset_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe ThemeAsset do +describe Locomotive::ThemeAsset do describe 'attaching a file' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5ef675c5..10fae0dd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,93 +1,41 @@ -# Note: if segmentation fault with spork / imagemagick on mac os x, take a look at: -# http://stackoverflow.com/questions/2838307/why-is-this-rmagick-call-generating-a-segmentation-fault +ENV["RAILS_ENV"] ||= 'test' -require 'rubygems' -require 'spork' +require 'rails/mongoid' +require File.join(File.dirname(__FILE__), 'dummy', 'config', 'environment.rb') +require 'rspec/rails' +require 'factory_girl' -# figure out where we are being loaded from -if $LOADED_FEATURES.grep(/spec\/spec_helper\.rb/).any? - begin - raise "foo" - rescue => e - puts <<-MSG - =================================================== - It looks like spec_helper.rb has been loaded - multiple times. Normalize the require to: +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir[File.join(File.dirname(__FILE__), 'support', '**', '*.rb')].each { |f| require f } - require "spec/spec_helper" +Locomotive.configure_for_test - Things like File.join and File.expand_path will - cause it to be loaded multiple times. +RSpec.configure do |config| - Loaded this time from: + config.include(Locomotive::RSpec::Matchers) - #{e.backtrace.join("\n ")} - =================================================== - MSG + config.mock_with :mocha + + config.before(:each) do + Locomotive.config.heroku = false end -end + require 'database_cleaner' + config.before(:suite) do + DatabaseCleaner.strategy = :truncation + DatabaseCleaner.orm = 'mongoid' + end -Spork.prefork do - # Loading more in this block will cause your tests to run faster. However, - # if you change any configuration or code from libraries loaded here, you'll - # 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 + 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 - -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 \ No newline at end of file diff --git a/spec/factories.rb b/spec/support/factories.rb similarity index 62% rename from spec/factories.rb rename to spec/support/factories.rb index 22eaf582..eb87fa35 100644 --- a/spec/factories.rb +++ b/spec/support/factories.rb @@ -1,22 +1,22 @@ FactoryGirl.define do ## Site ## - factory :site do + factory :site, :class => Locomotive::Site do name 'Acme Website' - subdomain 'acme' + # subdomain 'acme' created_at Time.now factory "test site" do name 'Locomotive test website' - subdomain 'test' + # subdomain '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 factory "another site" do name "Locomotive test website #2" - subdomain "test2" + # subdomain "test2" end end @@ -41,7 +41,7 @@ FactoryGirl.define do end # Accounts ## - factory :account do + factory :account, :class => Locomotive::Account do name 'Bart Simpson' email 'bart@simpson.net' password 'easyone' @@ -74,7 +74,7 @@ FactoryGirl.define do end ## Memberships ## - factory :membership do + factory :membership, :class => Locomotive::Membership do role 'admin' account { Account.where(:name => "Bart Simpson").first || Factory('admin user') } @@ -96,46 +96,49 @@ FactoryGirl.define do end ## Pages ## - factory :page do + factory :page, :class => Locomotive::Page do title 'Home page' slug 'index' published true - site { Site.where(:subdomain => "acme").first || Factory(:site) } + site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) } factory :sub_page do title 'Subpage' slug 'subpage' published true - site { Site.where(:subdomain => "acme").first || Factory(:site) } - parent { Page.where(:slug => "index").first || Factory(:page) } + site { Locomotive::Site.where(:subdomain => "acme").first || Factory(:site) } + parent { Locomotive::Page.where(:slug => "index").first || Factory(:page) } end end ## Snippets ## - factory :snippet do + factory :snippet, :class => Locomotive::Snippet do name 'My website title' slug 'header' template %{