From 77ea45c98b4fa64d4d68bbb8fd81a8a39a134350 Mon Sep 17 00:00:00 2001 From: did Date: Thu, 28 Apr 2011 23:47:01 +0200 Subject: [PATCH] solve a bug when creating new content instance + fix rspec tests for ruby 1.9.2 --- app/models/content_instance.rb | 1 + .../admin/shared/menu/_contents.html.haml | 2 +- spec/lib/locomotive/bushido_spec.rb | 13 +-- spec/lib/locomotive/heroku_spec.rb | 13 +-- spec/spec_helper.rb | 35 +------ spec/support/be_valid.rb | 31 ------ spec/support/matchers.rb | 98 +++++++++++++++++++ 7 files changed, 117 insertions(+), 76 deletions(-) delete mode 100644 spec/support/be_valid.rb create mode 100644 spec/support/matchers.rb diff --git a/app/models/content_instance.rb b/app/models/content_instance.rb index 659c0979..a14f620e 100644 --- a/app/models/content_instance.rb +++ b/app/models/content_instance.rb @@ -24,6 +24,7 @@ class ContentInstance after_create :send_notifications ## named scopes ## + scope :persisted, where(:updated_at.ne => nil) scope :latest_updated, :order_by => :updated_at.desc, :limit => Locomotive.config.lastest_items_nb ## methods ## diff --git a/app/views/admin/shared/menu/_contents.html.haml b/app/views/admin/shared/menu/_contents.html.haml index 946d23b2..94179ed0 100644 --- a/app/views/admin/shared/menu/_contents.html.haml +++ b/app/views/admin/shared/menu/_contents.html.haml @@ -15,7 +15,7 @@ .inner %h2!= t('admin.contents.index.lastest_items') %ul - - content_type.contents.latest_updated.each do |content| + - content_type.contents.persisted.latest_updated.each do |content| %li = link_to truncate(content.send(content_type.highlighted_field_name).to_s, :length => 20), edit_admin_content_path(content_type.slug, content) %span= time_ago_in_words(content.updated_at) diff --git a/spec/lib/locomotive/bushido_spec.rb b/spec/lib/locomotive/bushido_spec.rb index 79193f92..be3f0deb 100644 --- a/spec/lib/locomotive/bushido_spec.rb +++ b/spec/lib/locomotive/bushido_spec.rb @@ -17,9 +17,10 @@ describe 'Bushido support' do end it 'does not add instance methods to Site' do - Site.instance_methods.include?('add_bushido_domains').should be_false - Site.instance_methods.include?('remove_bushido_domains').should be_false - Site.methods.include?('create_first_one_with_bushido').should be_false + Site.should_not include_instance_method :add_bushido_domains + Site.should_not include_instance_method :remove_bushido_domains + + Site.should_not include_class_method :create_first_one_with_bushido end end @@ -37,8 +38,8 @@ describe 'Bushido support' do end it 'does not add methods to Site' do - Site.instance_methods.include?('add_bushido_domains').should be_false - Site.instance_methods.include?('remove_bushido_domains').should be_false + Site.should_not include_instance_method :add_bushido_domains + Site.should_not include_instance_method :remove_bushido_domains end end @@ -53,7 +54,7 @@ describe 'Bushido support' do it 'adds a method to automatically create a site with Bushido settings' do configure_locomotive_with_bushido - Site.methods.include?('create_first_one_with_bushido').should be_true + Site.should include_class_method :create_first_one_with_bushido end it 'tells bushido is enabled when forcing it' do diff --git a/spec/lib/locomotive/heroku_spec.rb b/spec/lib/locomotive/heroku_spec.rb index dce6ff66..899bd196 100644 --- a/spec/lib/locomotive/heroku_spec.rb +++ b/spec/lib/locomotive/heroku_spec.rb @@ -18,9 +18,10 @@ describe 'Heroku support' do end it 'does not add instance methods to Site' do - Site.instance_methods.include?('add_heroku_domains').should be_false - Site.instance_methods.include?('remove_heroku_domains').should be_false - Site.methods.include?('create_first_one_with_heroku').should be_false + Site.should_not include_instance_method :add_heroku_domains + Site.should_not include_instance_method :remove_heroku_domains + + Site.should_not include_class_method :create_first_one_with_heroku end end @@ -42,8 +43,8 @@ describe 'Heroku support' do end it 'does not add methods to Site' do - Site.instance_methods.include?('add_heroku_domains').should be_false - Site.instance_methods.include?('remove_heroku_domains').should be_false + Site.should_not include_instance_method :add_heroku_domains + Site.should_not include_instance_method :remove_heroku_domains end end @@ -58,7 +59,7 @@ describe 'Heroku support' do it 'adds a method to automatically create a site with Heroku settings' do configure_locomotive_with_heroku - Site.methods.include?('create_first_one_with_heroku').should be_true + Site.should include_class_method :create_first_one_with_heroku end it 'tells heroku is enabled when forcing it' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b7d0db24..dcd0a7c0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,9 @@ Dir[Rails.root.join('spec/support/**/*.rb')].each {|f| require f} Locomotive.configure_for_test RSpec.configure do |config| + + config.include(Locomotive::RSpec::Matchers) + # == Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: @@ -43,35 +46,3 @@ RSpec.configure do |config| end end end - - -# # This file is copied to ~/spec when you run 'ruby script/generate rspec' -# # from the project root directory. -# ENV["RAILS_ENV"] ||= 'test' -# require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT) -# require 'rspec/rails' -# -# # Requires supporting files with custom matchers and macros, etc, -# # in ./support/ and its subdirectories. -# Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} -# -# Rspec.configure do |config| -# config.expect_with :rspec -# 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 -# end diff --git a/spec/support/be_valid.rb b/spec/support/be_valid.rb deleted file mode 100644 index e0d6e2e6..00000000 --- a/spec/support/be_valid.rb +++ /dev/null @@ -1,31 +0,0 @@ -module Spec - module Rails - module Matchers - class BeValid #:nodoc: - - def matches?(model) - @model = model - @model.errors.clear - @model.errors.empty? && @model.valid? - end - - def failure_message - "#{@model.class} expected to be valid but had errors:\n #{@model.errors.full_messages.join("\n ")}" - end - - def negative_failure_message - "#{@model.class} expected to be invalid but was valid.\n" - end - - def description - "be valid" - end - - end - - def be_valid - BeValid.new - end - end - end -end diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb new file mode 100644 index 00000000..ec5d8fbd --- /dev/null +++ b/spec/support/matchers.rb @@ -0,0 +1,98 @@ +module Locomotive + module RSpec + module Matchers + class BeValid #:nodoc: + + def matches?(model) + @model = model + @model.errors.clear + @model.errors.empty? && @model.valid? + end + + def failure_message + "#{@model.class} expected to be valid but had errors:\n #{@model.errors.full_messages.join("\n ")}" + end + + def negative_failure_message + "#{@model.class} expected to be invalid but was valid.\n" + end + + def description + "be valid" + end + + end + + def be_valid + BeValid.new + end + + class IncludeInstanceMethod #:nodoc: + + def initialize(meth) + @meth = meth + end + + def matches?(klass) + @klass = klass + if RUBY_VERSION =~ /1\.9/ + klass.instance_methods.include?(@meth.to_sym) == true + else + klass.instance_methods.include?(@meth.to_s) == true + end + end + + def failure_message + "#{@klass} expected to include the instance method #{@meth}" + end + + def negative_failure_message + "#{@klass} expected to not include the instance method #{@meth} but included it.\n" + end + + def description + "include instance method #{@meth}" + end + + end + + def include_instance_method(meth) + IncludeInstanceMethod.new(meth) + end + + class IncludeClassMethod #:nodoc: + + def initialize(meth) + @meth = meth + end + + def matches?(klass) + @klass = klass + if RUBY_VERSION =~ /1\.9/ + klass.methods.include?(@meth.to_sym) == true + else + klass.methods.include?(@meth.to_s) == true + end + end + + def failure_message + "#{@klass} expected to include the class method #{@meth}" + end + + def negative_failure_message + "#{@klass} expected to not include the class method #{@meth} but included it.\n" + end + + def description + "include class method #{@meth}" + end + + end + + def include_class_method(meth) + IncludeClassMethod.new(meth) + end + + end + end +end