blog_logic 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.1
1
+ 1.2.2
@@ -1,5 +1,5 @@
1
1
  class Blog
2
- include BlogLogic::Base
2
+ include LuckySneaks::StringExtensions
3
3
  include Mongoid::Document
4
4
  include Mongoid::Timestamps
5
5
 
@@ -14,15 +14,14 @@ class Blog
14
14
  field :posts_per_page, :type => Integer
15
15
  field :rss_enabled, :type => Boolean
16
16
  field :has_topics, :type => Boolean, :default => false
17
+ index :slug
17
18
  has_many :blog_categories
18
- has_many :posts
19
+ has_many :posts, :dependent => :destroy
19
20
 
20
21
  # Behavior =======================================================================================
21
- attr_accessor :desired_slug
22
- has_slug :desired_slug
23
-
24
- # Validations ====================================================================================
25
- validates_presence_of :title, :description, :desired_slug
22
+ before_save :set_slug
23
+ validates_presence_of :description, :title
24
+ validates_uniqueness_of :slug
26
25
 
27
26
  # Instance methods ===============================================================================
28
27
  def copy_posts
@@ -44,13 +43,13 @@ class Blog
44
43
  end
45
44
 
46
45
  def path
47
- "/#{self.slug}/".gsub(/\/\//,'/').gsub(/\/\//,'/')
46
+ "/#{self.slug}"
48
47
  end
49
48
 
50
49
  def posts_by_month
51
50
  dates = {}
52
51
  self.posts.published.each do |p|
53
- date = p.publication_date.to_s(:year_month)
52
+ date = p.publication_date.to_s :year_month
54
53
  dates[date] ||= {}
55
54
  dates[date][:full_date] ||= p.publication_date.to_s(:month_year)
56
55
  dates[date][:posts] ||= []
@@ -60,6 +59,12 @@ class Blog
60
59
  end
61
60
 
62
61
  def search(keyword)
63
- self.posts.published.where(:content => /#{keyword}/i)
62
+ self.posts.published.where :content => /#{keyword}/i
63
+ end
64
+
65
+ private
66
+
67
+ def set_slug
68
+ self.slug = self.title.to_s.to_url if self.slug.blank?
64
69
  end
65
- end
70
+ end
@@ -23,15 +23,14 @@ class Post
23
23
  field :state
24
24
  field :publication_date, :type => DateTime
25
25
  field :summary
26
-
27
- index :slug, :unique => false
26
+ index :slug
28
27
  index :state, :unique => false
29
-
30
28
  belongs_to :blog
31
29
  has_and_belongs_to_many :blog_categories
32
30
 
33
31
  # Behavior =======================================================================================
34
- after_create :set_slug
32
+ before_save :set_slug
33
+ validates_uniqueness_of :slug
35
34
 
36
35
  # Tanker =========================================================================================
37
36
  tankit 'idx' do
@@ -46,18 +45,12 @@ class Post
46
45
  after_save :update_tank_indexes
47
46
 
48
47
  # Validations ====================================================================================
49
- # class DesiredSlugPresenceAndUniquenessValidator < ActiveModel::EachValidator
50
- # def validate_each(object, attribute, value)
51
- # object.desired_slug = object.title unless object.desired_slug
52
- # if object.blog && object.blog.posts.map{|p| p.slug unless p == object}.include?(object.desired_slug)
53
- # object.errors[attribute] << (options[:message] || "must be unique.")
54
- # end
55
- # end
56
- # end
57
-
58
- # validates :desired_slug, :desired_slug_presence_and_uniqueness => true
59
- validates_presence_of :title
60
- validates_presence_of :content
48
+ validates_presence_of :content, :title
49
+
50
+ # Instance methods: Overrides ====================================================================
51
+ def publication_date
52
+ self[:publication_date] || self.published_at
53
+ end
61
54
 
62
55
  # Instance methods ===============================================================================
63
56
  def draft?
@@ -76,7 +69,7 @@ class Post
76
69
  end
77
70
 
78
71
  def humanize_path
79
- "/#{self.slug}".gsub('//', '/')
72
+ self.path
80
73
  end
81
74
 
82
75
  def my_index
@@ -90,7 +83,7 @@ class Post
90
83
  end
91
84
 
92
85
  def path
93
- "#{self.blog.humanize_path}#{self.humanize_path}".gsub('//', '/')
86
+ "#{self.blog.path}/#{self.slug}".gsub('//', '/')
94
87
  end
95
88
 
96
89
  def previous_post
@@ -115,11 +108,6 @@ class Post
115
108
  self.title
116
109
  end
117
110
 
118
- # Sets the slug for this locale. Slugs from the locale tree are used to build this locale's URL.
119
- def set_slug
120
- self.slug = self.title.to_s.to_url
121
- end
122
-
123
111
  def unpublish!
124
112
  self.update_attributes :state => 'draft'
125
113
  end
@@ -131,4 +119,10 @@ class Post
131
119
  def status
132
120
  self.state ? self.state.capitalize : 'Draft'
133
121
  end
122
+
123
+ private
124
+
125
+ def set_slug
126
+ self.slug = self.title.to_s.to_url if self.slug.blank?
127
+ end
134
128
  end
@@ -18,7 +18,7 @@
18
18
 
19
19
  <div class="form_column">
20
20
  <%= f.text_field :title, :label => 'Blog Title' -%>
21
- <%= f.text_field :desired_slug, :label => 'URL Slug', :value => @blog.slug || '/blog/' -%>
21
+ <%= f.text_field :slug, :value => @blog.slug -%>
22
22
  <%= f.select :posts_per_page, %w{10 20 30 50} -%>
23
23
  </div>
24
24
  <div class="form_column">
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "blog_logic"
8
- s.version = "1.2.1"
8
+ s.version = "1.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bantik"]
@@ -90,8 +90,6 @@ Gem::Specification.new do |s|
90
90
  "features/support/paths.rb",
91
91
  "init.rb",
92
92
  "lib/blog_logic.rb",
93
- "lib/blog_logic/base.rb",
94
- "lib/blog_logic/engine.rb",
95
93
  "lib/blog_logic/import.rb",
96
94
  "lib/blog_logic/railtie.rb",
97
95
  "lib/tasks/.gitkeep",
@@ -7,20 +7,20 @@ Feature: Browse blog
7
7
  I want to browse the blog
8
8
 
9
9
  Scenario: Access the blog page
10
- Given I have a blog
10
+ Given I have a blog "My Blog"
11
11
  Given the following blog posts exist:
12
12
  | title | content | summary | state |
13
13
  | Foo | Foo is foo. | First | published |
14
14
  | Bar | Bar is bar. | Second | draft |
15
15
  When I visit the blog page
16
- Then I should see "Default Blog"
16
+ Then I should see "My Blog"
17
17
  And I should see "My blog description"
18
18
  And I should see "Foo"
19
19
  And I should see "First"
20
20
  And I should not see "Bar"
21
21
 
22
22
  Scenario: View published blog post
23
- Given I have a blog
23
+ Given I have a blog "My Blog"
24
24
  Given the following blog posts exist:
25
25
  | title | content | summary | state |
26
26
  | Foo | Foo is foo. | First | published |
@@ -29,7 +29,7 @@ Feature: Browse blog
29
29
  Then I should see "Foo"
30
30
 
31
31
  Scenario: Search for blog posts
32
- Given I have a blog
32
+ Given I have a blog "My Blog"
33
33
  Given the following blog posts exist:
34
34
  | title | content | summary | state |
35
35
  | Foo | Foo is foo. | First | published |
@@ -41,7 +41,7 @@ Feature: Browse blog
41
41
  And I should see "First"
42
42
 
43
43
  Scenario: Access the archive
44
- Given I have a blog
44
+ Given I have a blog "My Blog"
45
45
  Given the following blog posts exist:
46
46
  | title | content | summary | state | publication_date |
47
47
  | Foo | Foo is foo. | First | published | 2011-03-22 |
@@ -50,7 +50,7 @@ Feature: Browse blog
50
50
  And I should see "Foo"
51
51
 
52
52
  Scenario: Access the RSS feed
53
- Given I have a blog
53
+ Given I have a blog "My Blog"
54
54
  Given the following blog posts exist:
55
55
  | title | content | summary | state | publication_date |
56
56
  | Foo | Foo is foo. | First | published | 2011-03-22 |
@@ -9,7 +9,6 @@ Feature: Manage Blog
9
9
  When go to the admin_blogs page
10
10
  And I follow "Set Up Blog"
11
11
  And I fill in "Blog Title" with "Adventures at 7-11"
12
- And I fill in "URL" with "7-11-adventures"
13
12
  And I fill in "Description" with "An epic tale of strange stuff that happens at my local convenience store."
14
13
  And I select "10" from "Posts Per Page"
15
14
  And I press "Save"
@@ -19,7 +18,7 @@ Feature: Manage Blog
19
18
  And I should see "10"
20
19
 
21
20
  Scenario: Edit a blog
22
- Given I have a blog
21
+ Given I have a blog "My Blog"
23
22
  When go to the admin_blogs page
24
23
  And I follow "Blog Configuration"
25
24
  Then I should see "Blog Details"
@@ -27,5 +26,4 @@ Feature: Manage Blog
27
26
  And I fill in "Blog Title" with "Adventures at Circle K"
28
27
  And I press "Save"
29
28
  Then I should see "Successfully modified the blog."
30
- And I should see "Adventures at Circle K"
31
-
29
+ And I should see "Adventures at Circle K"
@@ -4,14 +4,14 @@ Feature: Post to blog
4
4
  I want to create and manage blog posts
5
5
 
6
6
  Scenario: View empty list of blog posts
7
- Given I have a blog
7
+ Given I have a blog "My Blog"
8
8
  When go to the admin_blogs page
9
9
  And I follow "Manage Posts"
10
10
  Then I should see "Blog Posts"
11
11
  And I should see "No posts have been written yet."
12
12
 
13
13
  Scenario: View populated list of blog posts
14
- Given I have a blog
14
+ Given I have a blog "My Blog"
15
15
  Given the following blog posts exist:
16
16
  | title | content | state |
17
17
  | Foo | Foo is foo. | draft |
@@ -23,7 +23,7 @@ Feature: Post to blog
23
23
  And I should see "Bar"
24
24
 
25
25
  Scenario: Create a draft blog post
26
- Given I have a blog
26
+ Given I have a blog "My Blog"
27
27
  When go to the admin_blogs page
28
28
  And I follow "Manage Posts"
29
29
  And I follow "New Post..."
@@ -34,11 +34,12 @@ Feature: Post to blog
34
34
  And I press "Save"
35
35
  Then I should see "About This Post"
36
36
  And I should see "My First Post"
37
+ And post slug should be "my-first-post"
37
38
 
38
39
  Scenario: Attach an image to a blog post
39
40
 
40
41
  Scenario: Edit a blog post
41
- Given I have a blog
42
+ Given I have a blog "My Blog"
42
43
  Given the following blog posts exist:
43
44
  | title | content | state |
44
45
  | Foo | Foo is foo. | draft |
@@ -52,7 +53,7 @@ Feature: Post to blog
52
53
  And I should see "My Earliest Post"
53
54
 
54
55
  Scenario: Publish a blog post
55
- Given I have a blog
56
+ Given I have a blog "My Blog"
56
57
  Given the following blog posts exist:
57
58
  | title | content | state |
58
59
  | Foo | Foo is foo. | draft |
@@ -64,7 +65,7 @@ Feature: Post to blog
64
65
  And I should see "Published"
65
66
 
66
67
  Scenario: Delete a blog post
67
- Given I have a blog
68
+ Given I have a blog "My Blog"
68
69
  Given the following blog posts exist:
69
70
  | title | content | state |
70
71
  | Foo | Foo is foo. | draft |
@@ -14,9 +14,10 @@ When /^I visit the blog RSS feed$/ do
14
14
  visit Blog.first.feed_address
15
15
  end
16
16
 
17
- Given /^I have a blog$/ do
18
- Blog.destroy_all
19
- Blog.create(:title => 'Default Blog', :desired_slug => 'my-blog', :description => 'My blog description')
17
+ Given /^I have a blog "([^"]*)"$/ do |title|
18
+ Blog.delete_all
19
+ Post.delete_all
20
+ Blog.make :title => title
20
21
  end
21
22
 
22
23
  Given /the following blog posts exist:/ do |table|
@@ -24,5 +25,10 @@ Given /the following blog posts exist:/ do |table|
24
25
  end
25
26
 
26
27
  Given /^no blogs exist$/ do
27
- Blog.destroy_all
28
+ Blog.delete_all
29
+ Post.delete_all
28
30
  end
31
+
32
+ Then /^post slug should be "([^"]*)"$/ do |slug|
33
+ Blog.first.posts.last.slug.should == slug
34
+ end
@@ -6,6 +6,7 @@
6
6
 
7
7
  ENV["RAILS_ENV"] ||= "test"
8
8
  require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
9
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec/blueprints")
9
10
 
10
11
  require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
11
12
  require 'cucumber/rails/rspec'
@@ -1,13 +1,8 @@
1
1
  module BlogLogic
2
-
3
- require 'mongoid'
4
- require 'blog_logic/engine' if defined?(Rails)
5
- require 'blog_logic/railtie' if defined?(Rails)
6
- require 'blog_logic/base'
7
2
  require 'blog_logic/import'
3
+ require 'blog_logic/railtie'
8
4
 
9
5
  def self.setup
10
6
  yield self
11
7
  end
12
-
13
- end
8
+ end
@@ -1,11 +1,7 @@
1
1
  # NOTE: search-and-replace content> with contents> before importing
2
-
3
2
  module BlogLogic
4
-
5
3
  module Import
6
-
7
4
  class WordPress
8
-
9
5
  attr_accessor :source_file
10
6
 
11
7
  def initialize(file)
@@ -20,8 +16,7 @@ module BlogLogic
20
16
  :publication_date => post.publication_date,
21
17
  :content => post.content,
22
18
  :summary => post.content.truncate(255),
23
- :state => "published",
24
- :desired_slug => "#{post.title.to_s}"
19
+ :state => 'published'
25
20
  )
26
21
  end
27
22
  end
@@ -31,10 +26,10 @@ module BlogLogic
31
26
  if @posts.empty?
32
27
  raw_posts.each do |item|
33
28
  @posts << Post.new(
34
- :title => item.xpath(".//title").first.content,
35
- :date => item.xpath(".//pubDate").first.content,
36
- :content => item.xpath(".//contents").children.first.content,
37
- :summary => item.xpath(".//excerpt").first.try(:content)
29
+ :title => item.xpath('.//title').first.content,
30
+ :date => item.xpath('.//pubDate').first.content,
31
+ :content => item.xpath('.//contents').children.first.content,
32
+ :summary => item.xpath('.//excerpt').first.try(:content)
38
33
  )
39
34
  end
40
35
  end
@@ -46,7 +41,7 @@ module BlogLogic
46
41
  end
47
42
 
48
43
  def raw_posts
49
- self.source.xpath("//item")
44
+ self.source.xpath('//item')
50
45
  end
51
46
 
52
47
  class Post
@@ -58,9 +53,6 @@ module BlogLogic
58
53
  DateTime.parse(self.date)
59
54
  end
60
55
  end
61
-
62
56
  end
63
-
64
57
  end
65
-
66
58
  end
@@ -1,5 +1,3 @@
1
- require 'blog_logic'
2
- require 'rails'
3
1
  module BlogLogic
4
2
  class Railtie < Rails::Railtie
5
3
  rake_tasks do
@@ -1,18 +1,14 @@
1
- require 'machinist/mongoid'
2
- require 'sham'
3
1
  require 'faker'
2
+ require 'machinist/mongoid'
4
3
 
5
4
  Blog.blueprint do
6
- title { Faker::Lorem.words(5) * " " }
7
- desired_slug { "my-blog" }
8
- description { "My incredibly insightful blog" }
5
+ title { Faker::Lorem.words(5) * ' ' }
6
+ description { 'My blog description' }
9
7
  end
10
8
 
11
9
  Post.blueprint do
12
- title { Faker::Lorem.words(5) * " " }
13
- summary { Faker::Lorem.words(5) * " " }
14
- content { Faker::Lorem.words(5) * " " }
15
- slug { Faker::Lorem.words(5) * "-" }
16
- publication_date { Time.zone.now }
17
- state { 'published' }
10
+ title { Faker::Lorem.words(5) * ' ' }
11
+ content { Faker::Lorem.words(5) * ' ' }
12
+ publication_date { Time.zone.now }
13
+ state { 'published' }
18
14
  end
@@ -2,11 +2,11 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe Admin::PostsController do
4
4
  before :each do
5
- Blog.destroy_all
5
+ Blog.delete_all
6
+ Post.delete_all
6
7
  @blog = Blog.make
7
- @post = Post.make
8
- @blog.posts << @post
9
- @blog.save
8
+ @blog.posts.create Post.plan
9
+ @post = @blog.posts.first
10
10
  end
11
11
 
12
12
  it 'gets index action' do
@@ -6,12 +6,12 @@ describe Blog do
6
6
  @blog = Blog.make
7
7
  end
8
8
 
9
- it "returns its feed address" do
10
- @blog.feed_address.should == "/my-blog/feed.rss"
9
+ it "returns its path" do
10
+ @blog.humanize_path.should == "/#{@blog.title.to_url}"
11
11
  end
12
12
 
13
- it "returns its humanized path" do
14
- @blog.humanize_path.should == "/my-blog/"
13
+ it "returns its feed address" do
14
+ @blog.feed_address.should == "/#{@blog.title.to_url}/feed.rss"
15
15
  end
16
16
 
17
17
  it "retrieves posts by month" do
@@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
 
3
3
  describe Post do
4
4
  before :all do
5
+ Post.delete_all
5
6
  @blog = Blog.make
6
7
  end
7
8
 
@@ -9,19 +10,15 @@ describe Post do
9
10
  @blog.posts.new.draft?.should be_true
10
11
  end
11
12
 
12
- it 'returns its relative URL' do
13
- @blog.posts.new(:slug => 'foo').full_path.should == '/my-blog/foo'
14
- end
15
-
16
- it 'returns its formatted slug' do
17
- @blog.posts.new(:slug => 'foo').humanize_path.should == '/foo'
13
+ it 'returns its path' do
14
+ @blog.posts.new(:slug => 'foo').path.should == "/#{@blog.title.to_url}/foo"
18
15
  end
19
16
 
20
17
  it 'publishes, setting its publication date' do
21
18
  post = @blog.posts.create(Post.make.attributes)
22
19
  post.publish!
23
20
  post.status.should == 'Published'
24
- post.publication_date.to_s(:concise).should == Time.now.to_s(:concise)
21
+ post.published_at.to_s(:concise).should == Time.now.to_s(:concise)
25
22
  end
26
23
 
27
24
  it 'unpublishes, clearing its publication date' do
@@ -31,13 +28,12 @@ describe Post do
31
28
  post.status.should == 'Draft'
32
29
  end
33
30
 
34
- it 'defaults its desired slug to its title' do
31
+ it 'defaults slug to its title' do
35
32
  post = @blog.posts.new(
36
33
  :title => 'Test Post',
37
- :summary => 'Foo',
38
34
  :content => 'Bar'
39
35
  )
40
- post.save.should be_true
36
+ post.save
41
37
  post.slug.should == 'test-post'
42
38
  end
43
- end
39
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blog_logic
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 1
10
- version: 1.2.1
9
+ - 2
10
+ version: 1.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bantik
@@ -288,8 +288,6 @@ files:
288
288
  - features/support/paths.rb
289
289
  - init.rb
290
290
  - lib/blog_logic.rb
291
- - lib/blog_logic/base.rb
292
- - lib/blog_logic/engine.rb
293
291
  - lib/blog_logic/import.rb
294
292
  - lib/blog_logic/railtie.rb
295
293
  - lib/tasks/.gitkeep
@@ -1,41 +0,0 @@
1
- module BlogLogic
2
-
3
- module Base
4
-
5
- @@sluggable_attribute = nil
6
-
7
- module ClassMethods
8
- def has_slug(attr)
9
- self.send(:set_callback, :save, :before, Proc.new{|doc| doc.make_slug})
10
- end
11
- end
12
-
13
- def self.included(base)
14
- base.extend(ClassMethods)
15
- end
16
-
17
- def make_slug
18
- if self.desired_slug && ! self.desired_slug.blank?
19
- text = self.desired_slug
20
- elsif self.slug
21
- text = self.slug
22
- elsif self.respond_to?(:page_title)
23
- text = self.page_title.to_s.downcase
24
- elsif self.respond_to?(:name)
25
- text = self.name.to_s.downcase
26
- end
27
-
28
- # Translation borrowed from permalink_fu
29
- text = text.to_s
30
- text.gsub!(/[^\x00-\x7F]+/, '-') # Remove anything non-ASCII entirely (e.g. diacritics).
31
- text.gsub!(/[^\/\w_ \-]+/i, '-') # Remove unwanted chars.
32
- text.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
33
- text.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
34
- text.downcase!
35
- self.slug = text
36
-
37
- end
38
-
39
- end
40
-
41
- end
@@ -1,7 +0,0 @@
1
- require 'blog_logic'
2
- require 'rails'
3
-
4
- module BlogLogic
5
- class Engine < Rails::Engine
6
- end
7
- end