diff --git a/app/controllers/admin/content_controller.rb b/app/controllers/admin/content_controller.rb index a8e11c41b0..5a5f23e83d 100644 --- a/app/controllers/admin/content_controller.rb +++ b/app/controllers/admin/content_controller.rb @@ -14,14 +14,12 @@ def auto_complete_for_article_keywords def index @search = params[:search] ? params[:search] : {} - - @articles = Article.search_with_pagination(@search, {page: params[:page], per_page: this_blog.admin_display_elements}) + @articles = Article.search_with(@search).page(params[:page]).per(this_blog.admin_display_elements) if request.xhr? render partial: 'article_list', locals: { articles: @articles } else @article = Article.new(params[:article]) - end end diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 78ac2595e6..32d9377814 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -11,7 +11,7 @@ class Admin::PagesController < Admin::BaseController def index @search = params[:search] ? params[:search] : {} - @pages = Page.search_paginate(@search, :page => params[:page], :per_page => this_blog.admin_display_elements) + @pages = Page.search_with(@search).page(params[:page]).per(this_blog.admin_display_elements) end def new diff --git a/app/helpers/admin/base_helper.rb b/app/helpers/admin/base_helper.rb index 89e563e666..285ac95031 100644 --- a/app/helpers/admin/base_helper.rb +++ b/app/helpers/admin/base_helper.rb @@ -80,21 +80,25 @@ def task_overview content_tag :li, link_to(_('Back to list'), :action => 'index') end - def render_void_table(size, cols) - return unless size == 0 + def render_empty_table(cols) content_tag(:tr) do - content_tag(:td, _("There are no %s yet. Why don't you start and create one?", _(controller.controller_name)), { :colspan => cols}) + content_tag(:td, _("There are no %s yet. Why don't you start and create one?", _(controller.controller_name)), { colspan: cols}) end end + def render_void_table(size, cols) + return unless size == 0 + render_empty_table(cols) + end + def cancel_or_save(message=_("Save")) "#{cancel} #{_("or")} #{save(message)}" end - def get_short_url(item) - return "" if item.short_url.nil? - sprintf(content_tag(:small, "%s %s"), _("Short url:"), link_to(item.short_url, item.short_url)) - end + def get_short_url(item) + return "" if item.short_url.nil? + sprintf(content_tag(:small, "%s %s"), _("Short url:"), link_to(item.short_url, item.short_url)) + end def show_actions item content_tag(:div, { :class => 'action', :style => '' }) do @@ -156,12 +160,12 @@ def render_macros(macros) def build_editor_link(label, action, id, update, editor) link = link_to_remote(label, - :url => { :action => action, 'editor' => editor}, - :method => :get, - :class => 'ui-button-text', - :loading => "new Element.show('update_spinner_#{id}')", - :success => "new Element.toggle('update_spinner_#{id}')", - :update => "#{update}") + :url => { :action => action, 'editor' => editor}, + :method => :get, + :class => 'ui-button-text', + :loading => "new Element.show('update_spinner_#{id}')", + :success => "new Element.toggle('update_spinner_#{id}')", + :update => "#{update}") link << image_tag("spinner-blue.gif", :id => "update_spinner_#{id}", :style => 'display:none;') end diff --git a/app/models/article.rb b/app/models/article.rb index 9bd003acb7..0fe9b2cec9 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -113,23 +113,18 @@ def self.last_draft(article_id) article end - def self.search_with_pagination(search_hash, paginate_hash) - state = (search_hash[:state] and ["no_draft", "drafts", "published", "withdrawn", "pending"].include? search_hash[:state]) ? search_hash[:state] : nil - - if state.nil? - list_function = function_search_all_posts(search_hash) - elsif - list_function = ["Article.#{state}"] + function_search_all_posts(search_hash) + def self.search_with(params) + params ||= {} + scoped = super(params) + if ["no_draft", "drafts", "published", "withdrawn", "pending"].include?(params[:state]) + scoped = scoped.send(params[:state]) end - if search_hash[:category] && search_hash[:category].to_i > 0 - list_function << 'category(search_hash[:category])' + if params[:category] && params[:category].to_i > 0 + scoped = scoped.category(params[:category]) end - list_function << "page(paginate_hash[:page])" - list_function << "per(paginate_hash[:per_page])" - list_function << "order('published_at desc, created_at desc')" - eval(list_function.join('.')) + scoped.order('published_at DESC').order('created_at DESC') end def permalink_url(anchor=nil, only_path=false) diff --git a/app/models/content.rb b/app/models/content.rb index 6222689203..70b0595a72 100644 --- a/app/models/content.rb +++ b/app/models/content.rb @@ -30,7 +30,6 @@ class Content < ActiveRecord::Base } scope :already_published, lambda { where('published = ? AND published_at < ?', true, Time.now).order(default_order) } - # Use only for self.function_search_all_posts method scope :published_at_like, lambda { |date_at| where(:published_at => ( if date_at =~ /\d{4}-\d{2}-\d{2}/ DateTime.strptime(date_at, '%Y-%m-%d').beginning_of_day..DateTime.strptime(date_at, '%Y-%m-%d').end_of_day @@ -67,28 +66,27 @@ def self.find_already_published(limit) where('published_at < ?', Time.now).limit(1000).order('created_at DESC') end - def self.function_search_all_posts(search_hash) - list_function = [] - search_hash ||= {} - - if search_hash[:searchstring] - list_function << 'searchstring(search_hash[:searchstring])' unless search_hash[:searchstring].to_s.empty? + def self.search_with(params) + params ||= {} + scoped = self.unscoped + if params[:searchstring].present? + scoped = scoped.searchstring(params[:searchstring]) end - if search_hash[:published_at] and %r{(\d\d\d\d)-(\d\d)} =~ search_hash[:published_at] - list_function << 'published_at_like(search_hash[:published_at])' + if params[:published_at].present? && %r{(\d\d\d\d)-(\d\d)} =~ params[:published_at] + scoped = scoped.published_at_like(params[:published_at]) end - if search_hash[:user_id] && search_hash[:user_id].to_i > 0 - list_function << 'user_id(search_hash[:user_id])' + if params[:user_id].present? && params[:user_id].to_i > 0 + scoped = scoped.user_id(params[:user_id]) end - if search_hash[:published] - list_function << 'published' if search_hash[:published].to_s == '1' - list_function << 'not_published' if search_hash[:published].to_s == '0' + if params[:published].present? + scoped = scoped.published if params[:published].to_s == '1' + scoped = scoped.not_published if params[:published].to_s == '0' end - list_function + scoped end def whiteboard diff --git a/app/models/page.rb b/app/models/page.rb index 5c38abc597..76d8a2dc77 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -31,12 +31,8 @@ def self.default_order 'name ASC' end - def self.search_paginate(search_hash, paginate_hash) - list_function = ["Page"] + function_search_all_posts(search_hash) - paginate_hash[:order] = 'title ASC' - list_function << "page(paginate_hash[:page])" - list_function << "per(paginate_hash[:per_page])" - eval(list_function.join('.')) + def self.search_with(search_hash) + super(search_hash).order('title ASC') end def permalink_url(anchor=nil, only_path=false) diff --git a/app/views/admin/content/_article_list.html.erb b/app/views/admin/content/_article_list.html.erb index 5336e9a33d..de3d18e109 100644 --- a/app/views/admin/content/_article_list.html.erb +++ b/app/views/admin/content/_article_list.html.erb @@ -1,4 +1,4 @@ -<%= render_void_table(@articles.size, 7) %> +<%= render_empty_table(7) if @articles.empty? %> <% for article in @articles %> > diff --git a/app/views/admin/content/index.html.erb b/app/views/admin/content/index.html.erb index 8af3b41ab2..700e55309f 100644 --- a/app/views/admin/content/index.html.erb +++ b/app/views/admin/content/index.html.erb @@ -1,13 +1,6 @@ <% @page_heading = _('Manage articles') + content_tag(:div, link_to(_("New Article"), {:controller => 'content', :action => 'new'}, :id => 'dialog-link', :class => 'btn btn-info'), :class => 'pull-right') %> -<%= form_remote_tag \ - :url => {:action => 'index'}, - :method => :get, - :name => 'article', - :update => {:success => 'articleList'}, - :before => "Element.show('spinner')", - :complete => "Element.hide('spinner')" \ - do %> +<%= form_remote_tag url: {action: 'index'}, method: :get, name: 'article', update: {:success => 'articleList'}, before: "Element.show('spinner')", complete: "Element.hide('spinner')" do %> <% if params[:search] and params[:search]['state'] %> @@ -15,26 +8,26 @@
- <%= link_to(_("All articles"), :action => 'index') %> + <%= link_to(_("All articles"), action: 'index') %> - <%= link_to(_("Published"), :action => 'index', :search => {:state => 'published'}) %> + <%= link_to(_("Published"), action: 'index', search: {state: 'published'}) %> - <%= link_to(_("Withdrawn"), :action => 'index', :search => {:state => 'withdrawn'}) %> + <%= link_to(_("Withdrawn"), action: 'index', search: {state: 'withdrawn'}) %> - <%= link_to(_("Drafts"), :action => 'index', :search => {:state => 'drafts'}) %> + <%= link_to(_("Drafts"), action: 'index', search: {state: 'drafts'}) %> - <%= link_to(_("Publication pending"), :action => 'index', :search => {:state => 'pending'}) %> + <%= link_to(_("Publication pending"), action: 'index', search: {state: 'pending'}) %>
<%= submit_tag(_("Search"), {:class => 'btn'}) %> - +

@@ -53,7 +46,7 @@ <%= _("Author")%> <%= _("Date") %> <%= _("Feedback")%> - + <%= render 'article_list', { :articles => @articles } -%> diff --git a/spec/controllers/admin/content_controller_spec.rb b/spec/controllers/admin/content_controller_spec.rb index 63dc1edfb2..ed99a627e0 100644 --- a/spec/controllers/admin/content_controller_spec.rb +++ b/spec/controllers/admin/content_controller_spec.rb @@ -19,8 +19,8 @@ end it 'should restrict only by searchstring' do - article = FactoryGirl.create(:article, :body => 'once uppon an originally time') - get :index, :search => {:searchstring => 'originally'} + article = create(:article, body: 'once uppon an originally time') + get :index, search: {searchstring: 'originally'} assigns(:articles).should == [article] response.should render_template('index') response.should be_success diff --git a/spec/controllers/admin/pages_controller_spec.rb b/spec/controllers/admin/pages_controller_spec.rb index 0d6434262d..ba0a2b7021 100644 --- a/spec/controllers/admin/pages_controller_spec.rb +++ b/spec/controllers/admin/pages_controller_spec.rb @@ -4,120 +4,126 @@ describe Admin::PagesController do render_views - before do - @blog = create(:blog) - @henri = create(:user, - login: 'henri', - profile: create(:profile_admin)) - request.session = { user: @henri.id } + def base_page(options={}) + { :title => "posted via tests!", + :body => "A good body", + :name => "posted-via-tests", + :published => true }.merge(options) end - describe '#index' do - it 'should response success' do - get :index - expect(response).to be_success - expect(response).to render_template('index') - expect(assigns(:pages)).to_not be_nil - end + let!(:blog) { create(:blog) } + + let(:user) { create(:user, profile: create(:profile_admin)) } + before(:each) { request.session = { user: user.id } } - it 'should response success with :page args' do - get :index, page: 1 - expect(response).to be_success - expect(response).to render_template('index') - expect(assigns(:pages)).to_not be_nil + describe :index do + context "without params" do + before(:each) { get :index } + it { expect(response).to be_success } + it { expect(response).to render_template('index') } + it { expect(assigns(:pages)).to_not be_nil } end + context "with page 1" do + before(:each) { get :index, page: 1 } + it { expect(response).to be_success } + it { expect(response).to render_template('index') } + it { expect(assigns(:pages)).to_not be_nil } + end end - describe "new" do - context "without page params" do - it "should render template new and has a page object" do - get :new - expect(response).to be_success - expect(response).to render_template("new") - expect(assigns(:page)).to_not be_nil + describe :new do + context "using get" do + context "without page params" do + it "should render template new and has a page object" do + get :new + expect(response).to be_success + expect(response).to render_template("new") + expect(assigns(:page)).to_not be_nil + end + + it "should assign to current user" do + get :new + expect(assigns(:page).user).to eq(user) + end + + it "should have a text filter" do + get :new + assigns(:page).text_filter.name.should eq 'textile' + end end + end - it "should assign to current user" do - get :new - expect(assigns(:page).user).to eq(@henri) - end + context "using post" do + it "test_create" do + post :new, :page => { :name => "new_page", :title => "New Page Title", + :body => "Emphasis _mine_, arguments *strong*" } - it "should have a text filter" do - get :new - assigns(:page).text_filter.name.should eq 'textile' - end - end - end + new_page = Page.find(:first, :order => "id DESC") - it "test_create" do - post :new, :page => { :name => "new_page", :title => "New Page Title", - :body => "Emphasis _mine_, arguments *strong*" } + assert_equal "new_page", new_page.name - new_page = Page.find(:first, :order => "id DESC") + assert_response :redirect, :action => "show", :id => new_page.id - assert_equal "new_page", new_page.name + # XXX: The flash is currently being made available improperly to tests (scoop) + assert_equal "Page was successfully created.", flash[:notice] + end - assert_response :redirect, :action => "show", :id => new_page.id + it 'should create a published page with a redirect' do + post(:new, 'page' => base_page) + assigns(:page).redirects.count.should == 1 + end - # XXX: The flash is currently being made available improperly to tests (scoop) - assert_equal "Page was successfully created.", flash[:notice] - end + it 'should create an unpublished page without a redirect' do + post(:new, 'page' => base_page({:published => false})) + assigns(:page).redirects.count.should == 0 + end - describe "test_edit" do - before(:each) do - @page = FactoryGirl.create(:page) - get :edit, :id => @page.id - end + it 'should create a page published in the future without a redirect' do + pending ":published_at parameter is currently ignored" + post(:new, 'page' => base_page({:published_at => (Time.now + 1.hour).to_s})) + assigns(:page).redirects.count.should == 0 + end - it 'should render edit template' do - assert_response :success - assert_template "edit" - assert_not_nil assigns(:page) - assert_equal @page, assigns(:page) end - end - it 'test_update' do - page = FactoryGirl.create(:page) - post :edit, :id => page.id, :page => { :name => "markdown-page", :title => "Markdown Page", - :body => "Adding a [link](http://www.publify.co/) here" } - - assert_response :redirect, :action => "show", :id => page.id - - # XXX: The flash is currently being made available improperly to tests (scoop) - #assert_equal "Page was successfully updated.", flash[:notice] - end + describe :edit do + context "using get" do + before(:each) do + @page = FactoryGirl.create(:page) + get :edit, :id => @page.id + end - it "test_destroy" do - page = FactoryGirl.create(:page) - post :destroy, :id => page.id - assert_response :redirect, :action => "list" - assert_raise(ActiveRecord::RecordNotFound) { Page.find(page.id) } - end + it 'should render edit template' do + assert_response :success + assert_template "edit" + assert_not_nil assigns(:page) + assert_equal @page, assigns(:page) + end - def base_page(options={}) - { :title => "posted via tests!", - :body => "A good body", - :name => "posted-via-tests", - :published => true }.merge(options) - end + end - it 'should create a published page with a redirect' do - post(:new, 'page' => base_page) - assigns(:page).redirects.count.should == 1 - end + context "using post" do + it 'test_update' do + page = FactoryGirl.create(:page) + post :edit, :id => page.id, :page => { :name => "markdown-page", :title => "Markdown Page", + :body => "Adding a [link](http://www.publify.co/) here" } - it 'should create an unpublished page without a redirect' do - post(:new, 'page' => base_page({:published => false})) - assigns(:page).redirects.count.should == 0 + assert_response :redirect, :action => "show", :id => page.id + # XXX: The flash is currently being made available improperly to tests (scoop) + #assert_equal "Page was successfully updated.", flash[:notice] + end + end end - it 'should create a page published in the future without a redirect' do - pending ":published_at parameter is currently ignored" - post(:new, 'page' => base_page({:published_at => (Time.now + 1.hour).to_s})) - assigns(:page).redirects.count.should == 0 + describe :destroy do + it "test_destroy" do + page = FactoryGirl.create(:page) + post :destroy, :id => page.id + assert_response :redirect, :action => "list" + assert_raise(ActiveRecord::RecordNotFound) { Page.find(page.id) } + end end describe 'insert_editor action' do @@ -137,3 +143,4 @@ def base_page(options={}) end end end + diff --git a/spec/factories.rb b/spec/factories.rb index 795a1851e6..8ea981bb5d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -2,39 +2,16 @@ # Factory definitions FactoryGirl.define do - sequence :name do |n| - "name_#{n}" - end - - sequence :user do |n| - "user#{n}" - end - - sequence :email do |n| - "user#{n}@example.com" - end - - sequence :guid do |n| - "deadbeef#{n}" - end - - sequence :label do |n| - "lab_#{n}" - end - - sequence :file_name do |f| - "file_name_#{f}" - end - - sequence :category do |n| - "c_#{n}" - end - - basetime = Time.now - sequence :time do |n| - basetime - n - end + sequence :name do |n|; "name_#{n}"; end + sequence :body do |n|; "body #{n}" * (n+3 % 5) ; end + sequence :user do |n|; "user#{n}" ; end + sequence :email do |n|; "user#{n}@example.com" ; end + sequence :guid do |n|; "deadbeef#{n}" ; end + sequence :label do |n|; "lab_#{n}" ; end + sequence :file_name do |f|; "file_name_#{f}" ; end + sequence :category do |n|; "c_#{n}" ; end + sequence :time do |n|; DateTime.new(2012,3,26,19,56) - n ; end factory :user do login { FactoryGirl.generate(:user) } @@ -241,9 +218,9 @@ end factory :page do - name 'page_one' + name {FactoryGirl.generate(:name)} title 'Page One Title' - body 'ho ho ho' + body {FactoryGirl.generate(:body)} created_at '2005-05-05 01:00:01' published_at '2005-05-05 01:00:01' updated_at '2005-05-05 01:00:01' @@ -262,7 +239,7 @@ state 'published' text_filter {FactoryGirl.create(:markdown)} end - + factory :unpublished_note, :parent => :note do |n| n.published false end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 4bfd25b5de..69a1f1e945 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -631,46 +631,50 @@ def assert_sets_trigger(art) end end - describe ".search_with_pagination" do - #TODO move those kind of test to a "integration specs" that can be run only for integration - context "given some datas" do - it "returns an empty array when no article and no params" do - Article.search_with_pagination({}, {page: nil, per_page: 12}).should be_empty - end - - it "returns article" do - article = FactoryGirl.create(:article) - Article.search_with_pagination({}, {page: nil, per_page: 12}).should eq([article]) - end + describe :search_with do + subject { Article.search_with(params) } - it "returns only published article where search params ask about published state" do - published_article = FactoryGirl.create(:article, state: 'published') - article = FactoryGirl.create(:article, state: 'draft') - Article.search_with_pagination({state: 'published'}, {page: nil, per_page: 12}).should eq([published_article]) - end + context "without article" do + let(:params) { nil } + it { expect(subject).to be_empty } + end - it "returns only quantity of article ask in per_page" do - article = FactoryGirl.create(:article, state: 'published') - out_of_per_page_article = FactoryGirl.create(:article, state: 'draft') + context "with an article" do + let(:params) { nil } + let!(:article) { create(:article) } + it { expect(subject).to eq([article]) } + end - Article.search_with_pagination({}, { page: nil, per_page: 1 }).should have(1).item - end + context "with two article but only one matching searchstring" do + let(:params) { {searchstring: 'match the string'} } + let!(:not_found_article) { create(:article) } + let!(:article) { create(:article, body: 'this match the string of article') } + it { expect(subject).to eq([article]) } + end - it "returns both published and draft articles by default" do - article = FactoryGirl.create(:article, state: 'published') - draft_article = FactoryGirl.create(:article, state: 'draft') - result = Article.search_with_pagination({}, {page: nil, per_page: 12}) - result.count.should eq 2 - end + context "with two articles with differents states and published params" do + let(:params) { {state: 'published'} } + let!(:article) { create(:article, state: 'published') } + let!(:draft_article) { create(:article, state: 'draft') } + it { expect(subject).to eq([article]) } + end - it "returns article of search categorie" do - show_category = FactoryGirl.create(:category, name: 'show') - hide_category = FactoryGirl.create(:category, name: 'not_show') - article = FactoryGirl.create(:article, categories: [show_category]) - hide_article = FactoryGirl.create(:article, categories: [hide_category]) - Article.search_with_pagination({category: show_category.id}, {page: nil, per_page: 12}).should eq([article]) - end + context "with two articles with differents states and no params" do + let(:params) { nil } + let(:now) { DateTime.new(2011,3,12) } + let!(:article) { create(:article, state: 'published', created_at: now) } + let!(:last_draft_article) { create(:article, state: 'draft', created_at: now + 2.days, published_at: nil) } + let!(:draft_article) { create(:article, state: 'draft', created_at: now + 20.days, published_at:nil) } + it { expect(subject).to eq([article, draft_article, last_draft_article]) } + end + context "with two articles in two catageory" do + let(:show_category) { create(:category, name: 'show') } + let(:not_show_category) { create(:category, name: 'not_show') } + let(:params) { {category: show_category.id } } + let!(:article) { create(:article, categories: [show_category]) } + let!(:bad_category_article) { create(:article, categories: [not_show_category]) } + it { expect(subject).to eq([article]) } end end diff --git a/spec/models/content_spec.rb b/spec/models/content_spec.rb index f5b5113ed5..3804822483 100644 --- a/spec/models/content_spec.rb +++ b/spec/models/content_spec.rb @@ -2,9 +2,7 @@ require 'spec_helper' describe Content do - before do - @blog = stub_default_blog - end + let!(:blog) { create(:blog) } describe "#short_url" do before do @@ -15,9 +13,7 @@ end describe "normally" do - before do - @blog.stub(:base_url) { "http://myblog.net" } - end + let!(:blog) { create(:blog, base_url: "http://myblog.net") } it "returns the blog's base url combined with the redirection's from path" do @content.should be_published @@ -26,9 +22,7 @@ end describe "when the blog is in a sub-uri" do - before do - @blog.stub(:base_url) { "http://myblog.net/blog" } - end + let!(:blog) { create(:blog, base_url: "http://myblog.net/blog") } it "includes the sub-uri path" do @content.short_url.should == "http://myblog.net/blog/foo" @@ -58,36 +52,48 @@ end end - describe "#function_search_all_posts" do - it "returns empty array when nil given" do - Content.function_search_all_posts(nil).should be_empty - end + describe :search_posts_with do + context "with an simple article" do + subject { Content.search_with(params) } - it "returns article that match with searchstring" do - expected_function = ['searchstring(search_hash[:searchstring])'] - Content.function_search_all_posts({searchstring: 'something'}).should eq expected_function - end + context "with nil params" do + let(:params) { nil } + it { expect(subject).to be_empty } + end - it "returns article that match with published_at" do - expected_function = ['published_at_like(search_hash[:published_at])'] - Content.function_search_all_posts({published_at: '2012-02'}).should eq expected_function - end + context "with a matching searchstring article" do + let(:params) { {searchstring: 'a search string'} } + let!(:match_article) { create(:article, body: 'there is a search string here') } + it { expect(subject).to eq([match_article]) } + end - it "returns article that match with user_id" do - expected_function = ['user_id(search_hash[:user_id])'] - Content.function_search_all_posts({user_id: '1'}).should eq expected_function - end + context "with an article published_at" do + let(:params) { {published_at: '2012-02'} } + let!(:article) { create(:article) } + let!(:match_article) { create(:article, published_at: DateTime.new(2012,2,13)) } + it { expect(subject).to eq([match_article]) } + end - it "returns article that match with not published" do - expected_function = ['not_published'] - Content.function_search_all_posts({published: '0'}).should eq expected_function - end + context "with same user_id article" do + let(:params) { {user_id: '13'} } + let!(:article) { create(:article) } + let!(:match_article) { create(:article, user_id: 13) } + it { expect(subject).to eq([match_article]) } + end - it "returns article that match with published" do - expected_function = ['published'] - Content.function_search_all_posts({published: '1'}).should eq expected_function - end + context "with not published status article" do + let(:params) { {published: '0' } } + let!(:article) { create(:article) } + let!(:match_article) { create(:article, published: false) } + it { expect(subject).to eq([match_article]) } + end + context "with published status article" do + let(:params) { {published: '1' } } + let!(:article) { create(:article, published: true) } + it { expect(subject).to eq([article]) } + end + end end end diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index 9d689394a0..42bddc84c0 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -2,126 +2,82 @@ require 'spec_helper' describe Page do - describe "#initialize" do - it "accepts a settings field in its parameter hash" do - Page.new({"password" => 'foo'}) + let!(:blog) { create(:blog) } + + describe :name= do + context "when build a page without name" do + let(:page) { create(:page, name: nil, title: 'A title') } + it { expect(page.name).to eq('a-title') } end end -end -describe "Testing redirects" do - it "a new published page gets a redirect" do - FactoryGirl.create(:blog) - a = Page.create(:title => "Some title", :body => "some text", :published => true) - a.should be_valid - a.redirects.first.should_not be_nil - a.redirects.first.to_path.should == a.permalink_url + describe :permalink do + context "with an existing page" do + let(:page) { create(:page, name: 'page_one') } + it { expect(page.permalink_url).to eq('http://myblog.net/pages/page_one') } + end end - it "a new unpublished page should not get a redirect" do - FactoryGirl.create(:blog) - a = Page.create(:title => "Some title", :body => "some text", :published => true) - a = Page.create(:title => "Another title", :body => "some text", :published => false) - a.redirects.first.should be_nil - end + describe "validations" do + context "with an existing page name" do + let!(:page) { create(:page, name: 'page_one') } + it { expect(build(:page, name: page.name)).to be_invalid } + end - it "Changin a published article permalink url should only change the to redirection" do - FactoryGirl.create(:blog) - a = Page.create(:title => "Third title", :body => "some text", :published => true) - a.should be_valid - a.redirects.first.should_not be_nil - a.redirects.first.to_path.should == a.permalink_url - r = a.redirects.first.from_path - - a.name = "some-new-permalink" - a.save - a.redirects.first.should_not be_nil - a.redirects.first.to_path.should == a.permalink_url - a.redirects.first.from_path.should == r - end -end + context "without name" do + it { expect(build(:page, name: nil)).to be_valid } + end -describe 'Given the fixture :first_page' do - before(:each) do - FactoryGirl.create(:blog) - @page = FactoryGirl.create(:page) - end + context "without body" do + it { expect(build(:page, body: nil)).to be_invalid } + end - describe "#permalink_url" do - subject { @page.permalink_url } - it { should == 'http://myblog.net/pages/page_one' } + context "without title" do + it { expect(build(:page, title: nil)).to be_invalid } + end end - it 'Pages cannot have the same name' do - Page.new(:name => @page.name, :body => @page.body, :title => @page.title).should_not be_valid - Page.new(:name => @page.name, :body => 'body', :title => 'title').should_not be_valid - end - - it "should give a sanitized title" do - page = FactoryGirl.build(:page, :title => 'title with accents éèà') - page.title.to_permalink.should == 'title-with-accents-eea' + describe :default_text_filter do + it { expect(create(:page).default_text_filter.name).to eq(blog.text_filter) } end -end -class Hash - def except(*keys) - self.reject { |k,v| keys.include? k.to_sym } - end + describe :search_with do + context "with an simple page" do + let!(:page) { create(:page) } - def only(*keys) - self.dup.reject { |k, v| !keys.include? k.to_sym } - end -end + subject { Page.search_with(params) } -describe 'Given no pages' do - def valid_attributes - { :title => 'title', :body => 'body'} - end + context "with nil params" do + let(:params) { nil } + it { expect(subject).to eq([page]) } + end - before(:each) do - Page.delete_all - @page = Page.new - end + context "with a matching searchstring page" do + let(:params) { {searchstring: 'foobar'} } + let!(:match_page) { create(:page, title: 'foobar') } + it { expect(subject).to eq([match_page]) } + end - it 'An empty page is invalid' do - @page.should_not be_valid + context "with 2 pages with title aaa and zzz" do + let!(:last_page) { create(:page, title: 'ZZZ', published: true) } + let!(:first_page) { create(:page, title: 'AAA', published: true) } + let(:params) { {published: '1'} } + it { expect(subject).to eq([first_page, page, last_page]) } + end + end end - it 'A page is valid with a title and body' do - @page.attributes = valid_attributes - @page.should be_valid - end + describe :redirects do + context "with a simple page" do + let(:page) { create(:page) } + it { expect(page.redirects.first.to_path).to eq(page.permalink_url) } + end - it 'A page is invalid without a title' do - @page.attributes = valid_attributes.except(:title) - @page.should_not be_valid - @page.errors[:title].should == ["can't be blank"] - @page.title = 'sometitle' - @page.should be_valid - end + context "with an unpublished page" do + let(:page) { create(:page, published: false) } + it { expect(page.redirects).to be_empty} + end - it 'A page is invalid without a body' do - @page.attributes = valid_attributes.except(:body) - @page.should_not be_valid - @page.errors[:body].should == ["can't be blank"] - @page.body = 'somebody' - @page.should be_valid end - - it "should use sanitize title to set page name" do - @page.attributes = valid_attributes.except(:title) - @page.title = 'title with accents éèà' - @page.should be_valid - @page.save - @page.name.should == "title-with-accents-eea" - end - end -describe 'Given a valid page' do - it 'default filter should be fetched from the blog' do - FactoryGirl.create(:blog) - @page = Page.new() - @page.default_text_filter.name.should == Blog.default.text_filter - end -end