<commit_before>require 'spec_helper'

describe Page do

  describe 'attributes' do

    before :each do
      @root = Fabricate :page, :layout_name => 'foo_layout'
    end

    it 'should have nil slug location if root' do
      @root.slug.should == nil
      @root.location.should == nil
    end

    it 'should generate proper location' do
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar
      @foo.location.should == 'foo'
      @bar.location.should == 'foo/bar'
      @baz.location.should == 'foo/bar/baz'
    end

    it 'should check slug uniqueness' do
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'foo', :parent => @foo
      @baz = Fabricate :page, :slug => 'foo', :parent => @root
      @foo.should be_valid
      @bar.should be_valid
      @baz.should_not be_valid
    end

    it 'should update descendants locations' do
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar

      @foo.update_attributes :slug => 'moo'

      @foo.reload.location.should == 'moo'
      @bar.reload.location.should == 'moo/bar'
      @baz.reload.location.should == 'moo/bar/baz'

      @bar.update_attributes :slug => 'fuu'

      @foo.reload.location.should == 'moo'
      @bar.reload.location.should == 'moo/fuu'
      @baz.reload.location.should == 'moo/fuu/baz'
    end

  end

  describe 'page parts' do

    before :each do
      @root = Fabricate :page, :layout_name => 'foo_layout'
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo, :page_parts => [Fabricate(:page_part, :name => PufferPages.primary_page_part_name, :body => '4')]
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar, :page_parts => [Fabricate(:page_part, :name => 'sidebar', :body => '5'), Fabricate(:page_part, :name => 'additional', :body => '3')]
      @foo.page_parts = [Fabricate(:page_part, :name => 'sidebar', :body => '2')]
    end

    it 'should create default `main` part for root page' do
      @root.page_parts.map(&:name).should == [PufferPages.primary_page_part_name]
    end

    it 'should receive proper inherited page parts' do
      @root.inherited_page_parts.map(&:body).should == [nil]
      @foo.inherited_page_parts.map(&:body).should == [nil, '2']
      @bar.inherited_page_parts.map(&:body).should == ['4', '2']
      @baz.inherited_page_parts.map(&:body).should == ['4', '3', '5']
    end

  end

  describe 'layout' do

    before :each do
      @layout = Fabricate :layout, :name => 'bar_layout'
      @root = Fabricate :page, :layout_name => 'foo_layout'
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo, :layout_name => 'bar_layout'
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar
    end

    it 'should receive proper layout name' do
      @root.inherited_layout_name.should == 'foo_layout'
      @foo.inherited_layout_name.should == 'foo_layout'
      @bar.inherited_layout_name.should == 'bar_layout'
      @baz.inherited_layout_name.should == 'bar_layout'
    end

    it 'should return proper layout for render' do
      @root.render_layout.should == 'foo_layout'
      @foo.render_layout.should == 'foo_layout'
      @bar.render_layout.should == false
      @baz.render_layout.should == false
    end

  end

  describe 'rendering' do

    before :each do
      @root = Fabricate :page, :layout_name => 'foo_layout'
      @main = Fabricate :page_part, :name => PufferPages.primary_page_part_name, :body => '{{ self.title }}'
      @sidebar = Fabricate :page_part, :name => 'sidebar', :body => '{{ self.name }}'
      @root.page_parts = [@main, @sidebar]
    end

    it 'should render content_for blocks if rails layout used' do
      result = @root.render 'self' => PufferPages::Liquid::PageDrop.new(@root)
      result.should == "#{@root.title}<% content_for :sidebar do %>#{@root.name}<% end %>"
    end

    it 'should render layout' do
      @layout = Fabricate :layout, :name => 'foo_layout', :body => "{% include 'body' %} {% include 'sidebar' %}"
      result = @root.render 'self' => PufferPages::Liquid::PageDrop.new(@root)
      result.should == "#{@root.title} #{@root.name}"
    end

    it 'should receive proper content type' do
      @root.content_type.should == 'text/html'
      page = Fabricate :page, :slug => 'style.css'
      page.content_type.should == 'text/css'
    end

  end

end
<commit_msg>Added specs<commit_after>require 'spec_helper'

describe Page do

  it 'should save root with empty or not slug' do
    @root = Fabricate :page, :layout_name => 'foo_layout'
    @root2 = Fabricate :page, :layout_name => 'foo_layout', :slug => 'foo'

    @root.new_record?.should be_false
    @root2.new_record?.should be_false
  end

  it 'should save non-root with not empty slug' do
    @root = Fabricate :page, :layout_name => 'foo_layout'
    @root2 = Fabricate :page, :layout_name => 'foo_layout', :slug => '', :parent => @root
    @root3 = Fabricate :page, :layout_name => 'foo_layout', :slug => 'bar', :parent => @root

    @root2.new_record?.should be_true
    @root3.new_record?.should be_false
  end

  describe 'attributes' do

    before :each do
      @root = Fabricate :page, :layout_name => 'foo_layout'
    end

    it 'should have nil slug location if root' do
      @root.slug.should == nil
      @root.location.should == nil
    end

    it 'should generate proper location' do
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar
      @foo.location.should == 'foo'
      @bar.location.should == 'foo/bar'
      @baz.location.should == 'foo/bar/baz'
    end

    it 'should check slug uniqueness' do
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'foo', :parent => @foo
      @baz = Fabricate :page, :slug => 'foo', :parent => @root
      @foo.should be_valid
      @bar.should be_valid
      @baz.should_not be_valid
    end

    it 'should update descendants locations' do
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar

      @foo.update_attributes :slug => 'moo'

      @foo.reload.location.should == 'moo'
      @bar.reload.location.should == 'moo/bar'
      @baz.reload.location.should == 'moo/bar/baz'

      @bar.update_attributes :slug => 'fuu'

      @foo.reload.location.should == 'moo'
      @bar.reload.location.should == 'moo/fuu'
      @baz.reload.location.should == 'moo/fuu/baz'
    end

  end

  describe 'page parts' do

    before :each do
      @root = Fabricate :page, :layout_name => 'foo_layout'
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo, :page_parts => [Fabricate(:page_part, :name => PufferPages.primary_page_part_name, :body => '4')]
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar, :page_parts => [Fabricate(:page_part, :name => 'sidebar', :body => '5'), Fabricate(:page_part, :name => 'additional', :body => '3')]
      @foo.page_parts = [Fabricate(:page_part, :name => 'sidebar', :body => '2')]
    end

    it 'should create default `main` part for root page' do
      @root.page_parts.map(&:name).should == [PufferPages.primary_page_part_name]
    end

    it 'should receive proper inherited page parts' do
      @root.inherited_page_parts.map(&:body).should == [nil]
      @foo.inherited_page_parts.map(&:body).should == [nil, '2']
      @bar.inherited_page_parts.map(&:body).should == ['4', '2']
      @baz.inherited_page_parts.map(&:body).should == ['4', '3', '5']
    end

  end

  describe 'layout' do

    before :each do
      @layout = Fabricate :layout, :name => 'bar_layout'
      @root = Fabricate :page, :layout_name => 'foo_layout'
      @foo = Fabricate :page, :slug => 'foo', :parent => @root
      @bar = Fabricate :page, :slug => 'bar', :parent => @foo, :layout_name => 'bar_layout'
      @baz = Fabricate :page, :slug => 'baz', :parent => @bar
    end

    it 'should receive proper layout name' do
      @root.inherited_layout_name.should == 'foo_layout'
      @foo.inherited_layout_name.should == 'foo_layout'
      @bar.inherited_layout_name.should == 'bar_layout'
      @baz.inherited_layout_name.should == 'bar_layout'
    end

    it 'should return proper layout for render' do
      @root.render_layout.should == 'foo_layout'
      @foo.render_layout.should == 'foo_layout'
      @bar.render_layout.should == false
      @baz.render_layout.should == false
    end

  end

  describe 'rendering' do

    before :each do
      @root = Fabricate :page, :layout_name => 'foo_layout'
      @main = Fabricate :page_part, :name => PufferPages.primary_page_part_name, :body => '{{ self.title }}'
      @sidebar = Fabricate :page_part, :name => 'sidebar', :body => '{{ self.name }}'
      @root.page_parts = [@main, @sidebar]
    end

    it 'should render content_for blocks if rails layout used' do
      result = @root.render 'self' => PufferPages::Liquid::PageDrop.new(@root)
      result.should == "#{@root.title}<% content_for :sidebar do %>#{@root.name}<% end %>"
    end

    it 'should render layout' do
      @layout = Fabricate :layout, :name => 'foo_layout', :body => "{% include 'body' %} {% include 'sidebar' %}"
      result = @root.render 'self' => PufferPages::Liquid::PageDrop.new(@root)
      result.should == "#{@root.title} #{@root.name}"
    end

    it 'should receive proper content type' do
      @root.content_type.should == 'text/html'
      page = Fabricate :page, :slug => 'style.css'
      page.content_type.should == 'text/css'
    end

  end

end

{'fraction_non_alphanumeric': 0.16427511133102424, 'fraction_numerical': 0.0025729836714497773, 'mean_word_length': 3.128267973856209, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4193703', 'n_tokens_mistral': 3519, 'n_tokens_neox': 3430, 'n_words': 874}