From 51248c60ff9eefa1f9ac0b30c47865d41d042303 Mon Sep 17 00:00:00 2001 From: yafei Lee Date: Sat, 23 Jun 2012 12:07:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8E=E5=8F=B0=E7=AE=A1?= =?UTF-8?q?=E7=90=86,=E5=B9=B6=E5=B0=86mongoid=E5=88=87=E5=9B=9E2.x?= =?UTF-8?q?=E7=89=88=E6=9C=AC,=20=E5=9B=A0=E4=B8=BArspec=E8=BF=87=E4=B8=8D?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 25 ++++------ Gemfile.lock | 62 +++++------------------- app/controllers/blogs_controller.rb | 1 + app/models/post.rb | 9 ++++ app/views/blogs/show.html.erb | 6 ++- config/mongoid.yml | 74 +++++++---------------------- config/routes.rb | 3 ++ spec/models/post_spec.rb | 5 +- spec/spec_helper.rb | 14 ++++-- 9 files changed, 69 insertions(+), 130 deletions(-) diff --git a/Gemfile b/Gemfile index da253f7..04fdd63 100644 --- a/Gemfile +++ b/Gemfile @@ -22,29 +22,20 @@ group :assets do end group :test do - gem 'capybara' - gem 'cucumber-rails' - gem 'factory_girl' - gem 'mongoid-rspec', :require => false + #gem 'capybara' + #gem 'cucumber-rails' + #gem 'factory_girl' + #gem 'mongoid-rspec', :require => false end group :test, :development do - gem 'rspec-rails' + gem "rspec-rails", ">= 2.8.1" end gem 'jquery-rails' -# To use ActiveModel has_secure_password -# gem 'bcrypt-ruby', '~> 3.0.0' - -# To use Jbuilder templates for JSON -# gem 'jbuilder' - -# Use unicorn as the app server -# gem 'unicorn' - -# Deploy with Capistrano # gem 'capistrano' -# To use debugger -# gem 'debugger' +gem "bluecloth" +gem "simple_form" +gem 'database_cleaner' diff --git a/Gemfile.lock b/Gemfile.lock index c8f817f..bd89bb4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,21 +28,12 @@ GEM activesupport (3.2.6) i18n (~> 0.6) multi_json (~> 1.0) - addressable (2.2.8) arel (3.0.2) + bluecloth (2.2.0) bson (1.6.4) bson_ext (1.6.4) bson (~> 1.6.4) builder (3.0.0) - capybara (1.1.2) - mime-types (>= 1.16) - nokogiri (>= 1.3.3) - rack (>= 1.0.0) - rack-test (>= 0.5.4) - selenium-webdriver (~> 2.0) - xpath (~> 0.1.4) - childprocess (0.3.2) - ffi (~> 1.0.6) coffee-rails (3.2.2) coffee-script (>= 2.2.0) railties (~> 3.2.0) @@ -50,24 +41,11 @@ GEM coffee-script-source execjs coffee-script-source (1.3.3) - cucumber (1.2.1) - builder (>= 2.1.2) - diff-lcs (>= 1.1.3) - gherkin (~> 2.11.0) - json (>= 1.4.6) - cucumber-rails (1.3.0) - capybara (>= 1.1.2) - cucumber (>= 1.1.8) - nokogiri (>= 1.5.0) + database_cleaner (0.8.0) diff-lcs (1.1.3) erubis (2.7.0) execjs (1.4.0) multi_json (~> 1.0) - factory_girl (3.5.0) - activesupport (>= 3.0.0) - ffi (1.0.11) - gherkin (2.11.0) - json (>= 1.4.6) hike (1.2.1) i18n (0.6.0) journey (1.0.4) @@ -75,26 +53,18 @@ GEM railties (>= 3.2.0, < 5.0) thor (~> 0.14) json (1.7.3) - libwebsocket (0.1.3) - addressable mail (2.4.4) i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.19) - mongoid (3.0.0.rc) + mongo (1.6.2) + bson (~> 1.6.2) + mongoid (2.4.11) activemodel (~> 3.1) - moped (~> 1.0.0.rc) - origin (~> 1.0.0.rc) + mongo (<= 1.6.2) tzinfo (~> 0.3.22) - mongoid-rspec (1.4.6) - mongoid (>= 3.0.0.rc) - rake - rspec (>= 2.9) - moped (1.0.0.rc) multi_json (1.3.6) - nokogiri (1.5.4) - origin (1.0.1) polyglot (0.3.3) rack (1.4.1) rack-cache (1.2) @@ -134,17 +104,14 @@ GEM activesupport (>= 3.0) railties (>= 3.0) rspec (~> 2.10.0) - rubyzip (0.9.9) sass (3.1.19) sass-rails (3.2.5) railties (~> 3.2.0) sass (>= 3.1.10) tilt (~> 1.3) - selenium-webdriver (2.24.0) - childprocess (>= 0.2.5) - libwebsocket (~> 0.1.3) - multi_json (~> 1.0) - rubyzip + simple_form (2.0.2) + actionpack (~> 3.0) + activemodel (~> 3.0) sprockets (2.1.3) hike (~> 1.2) rack (~> 1.0) @@ -158,22 +125,19 @@ GEM uglifier (1.2.5) execjs (>= 0.3.0) multi_json (~> 1.3) - xpath (0.1.4) - nokogiri (~> 1.3) PLATFORMS ruby DEPENDENCIES + bluecloth bson_ext - capybara coffee-rails (~> 3.2.1) - cucumber-rails - factory_girl + database_cleaner jquery-rails mongoid - mongoid-rspec rails (= 3.2.6) - rspec-rails + rspec-rails (>= 2.8.1) sass-rails (~> 3.2.3) + simple_form uglifier (>= 1.0.3) diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb index a776700..efe001d 100644 --- a/app/controllers/blogs_controller.rb +++ b/app/controllers/blogs_controller.rb @@ -4,5 +4,6 @@ class BlogsController < ApplicationController end def show + @post = Post.find(params[:id]) end end diff --git a/app/models/post.rb b/app/models/post.rb index d13c755..de050a8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,5 +1,14 @@ +# encoding : utf-8 class Post + TECH = "技术" + LIFE = "生活" + CREATOR = "创业" include Mongoid::Document field :title, :type => String field :content, :type => String + field :type, :type=> String + + validates :title, :presence=>true, :uniqueness=> true + validates :content, :presence=>true, :length => { :minimum=> 30 } + validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] } end diff --git a/app/views/blogs/show.html.erb b/app/views/blogs/show.html.erb index a8701a8..ef516c2 100644 --- a/app/views/blogs/show.html.erb +++ b/app/views/blogs/show.html.erb @@ -1,2 +1,4 @@ -

Blogs#show

-

Find me in app/views/blogs/show.html.erb

+
+

<%= @post.title %>

+
<%= @post.content %>
+
diff --git a/config/mongoid.yml b/config/mongoid.yml index 888aeee..57bbb37 100644 --- a/config/mongoid.yml +++ b/config/mongoid.yml @@ -1,60 +1,20 @@ development: - # Configure available database sessions. (required) - sessions: - # Defines the default session. (required) - default: - # Defines the name of the default database that Mongoid can connect to. - # (required). - database: w_blog_development - # Provides the hosts the default session can connect to. Must be an array - # of host:port pairs. (required) - hosts: - - localhost:27017 - options: - # Change whether the session persists in safe mode by default. - # (default: false) - # safe: false + host: localhost + database: w_blog_development - # Change the default consistency model to :eventual or :strong. - # :eventual will send reads to secondaries, :strong sends everything - # to master. (default: :eventual) - consistency: :strong - # Configure Mongoid specific options. (optional) - options: - # Configuration for whether or not to allow access to fields that do - # not have a field definition on the model. (default: true) - # allow_dynamic_fields: true +test: + host: localhost + database: w_blog_test - # Enable the identity map, needed for eager loading. (default: false) - # identity_map_enabled: false - - # Includes the root model name in json serialization. (default: false) - # include_root_in_json: false - - # Include the _type field in serializaion. (default: false) - # include_type_for_serialization: false - - # Preload all models in development, needed when models use - # inheritance. (default: false) - # preload_models: false - - # Protect id and type from mass assignment. (default: true) - # protect_sensitive_fields: true - - # Raise an error when performing a #find and the document is not found. - # (default: true) - # raise_not_found_error: true - - # Raise an error when defining a scope with the same name as an - # existing method. (default: false) - # scope_overwrite_exception: false - - # Skip the database version check, used when connecting to a db without - # admin access. (default: false) - # skip_version_check: false - - # User Active Support's time zone in conversions. (default: true) - # use_activesupport_time_zone: true - - # Ensure all times are UTC in the app side. (default: false) - # use_utc: false +# set these environment variables on your prod server +production: + host: <%= ENV['MONGOID_HOST'] %> + port: <%= ENV['MONGOID_PORT'] %> + username: <%= ENV['MONGOID_USERNAME'] %> + password: <%= ENV['MONGOID_PASSWORD'] %> + database: <%= ENV['MONGOID_DATABASE'] %> + # slaves: + # - host: slave1.local + # port: 27018 + # - host: slave2.local + # port: 27019 diff --git a/config/routes.rb b/config/routes.rb index db33895..0178772 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ WBlog::Application.routes.draw do root :to => 'blogs#index' resources :blogs, :only=>[:index, :show] + namespace :admin do + resources :posts + end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 98c3a24..1907dd1 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1,5 +1,8 @@ require 'spec_helper' describe Post do - pending "add some examples to (or delete) #{__FILE__}" + it "validates should be ok" do + a = Post.create!(title: 'one', content: '1'*31, type: Post::TECH ) + a.save.should == true + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f2ac851..fb22659 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,15 +13,21 @@ RSpec.configure do |config| # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: # clear test data - config.after :suite do - Mongoid.master.collections.select do |collection| - collection.name !~ /system/ - end.each(&:drop) + + config.before :each do + DatabaseCleaner.strategy = :truncation + DatabaseCleaner.start end + + config.after do + DatabaseCleaner.clean + end + # # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr + config.mock_with :rspec # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures #config.fixture_path = "#{::Rails.root}/spec/fixtures"