has_permalink 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/friendly_url.rb CHANGED
@@ -31,6 +31,7 @@ module FriendlyUrl
31
31
  n.gsub!(/[^\sa-z0-9_-]/, '')
32
32
  n.gsub!(/-{2,}/, '-')
33
33
  n.gsub!(/^-/, '')
34
+ n.gsub!(/-$/, '')
34
35
  n
35
36
  end
36
37
  end
data/lib/has_permalink.rb CHANGED
@@ -1,26 +1,38 @@
1
1
  require 'friendly_url'
2
2
 
3
3
  module HasPermalink
4
+ require 'railtie' if defined?(Rails)
5
+
4
6
  def has_permalink(generate_from = :title)
5
- unless included_modules.include? Behavior
7
+ unless included_modules.include? InstanceMethods
6
8
  class_inheritable_accessor :generate_from
7
- include Behavior
9
+ extend ClassMethods
10
+ include InstanceMethods
8
11
  end
9
12
 
10
13
  self.generate_from = generate_from
11
14
  before_validation :generate_permalink
12
15
  end
13
-
14
- module Behavior
16
+
17
+ module ClassMethods
18
+ def generate_permalinks
19
+ self.all.each do |item|
20
+ item.generate_permalink
21
+ item.save
22
+ end
23
+ end
24
+ end
25
+
26
+ module InstanceMethods
15
27
  include FriendlyUrl
16
28
  def generate_permalink
17
29
  self.permalink = normalize(self.send(generate_from)) if permalink.blank?
18
30
  end
19
-
31
+
20
32
  def generate_permalink!
21
33
  self.permalink = normalize(self.send(generate_from))
22
34
  end
23
-
35
+
24
36
  def to_param
25
37
  permalink
26
38
  end
data/lib/railtie.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'has_permalink'
2
+ require 'rails'
3
+
4
+ module HasPermalink
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load 'tasks/has_permalink.rake'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ namespace :has_permalink do
2
+ desc 'Generate permalinks for [MODEL]'
3
+ task :generate_permalinks, :model_name, :needs => :environment do |t, args|
4
+ begin
5
+ model_name = args.first[1]
6
+ rescue
7
+ puts "ERROR: You need to pass the name of the model as an argument."
8
+ puts "Try this: 'rake has_permalink:generate_permalinks[MODEL]'"
9
+ else
10
+ generate_permalinks(model_name)
11
+ end
12
+ end
13
+ end
14
+
15
+ def generate_permalinks(model_name)
16
+ begin
17
+ if Kernel.const_get(model_name)
18
+ model_name.constantize.generate_permalinks
19
+ puts "Congratulations! '#{model_name}' has permalinks!"
20
+ end
21
+ rescue
22
+ puts "Can't find model '#{model_name}'. Does it exist?"
23
+ end
24
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_permalink
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ola Karlsson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-31 00:00:00 +01:00
18
+ date: 2011-02-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,8 @@ files:
50
50
  - lib/generators/templates/has_permalink_migration.rb.erb
51
51
  - lib/generators/USAGE
52
52
  - lib/has_permalink.rb
53
+ - lib/railtie.rb
54
+ - lib/tasks/has_permalink.rake
53
55
  - rails/init.rb
54
56
  has_rdoc: true
55
57
  homepage: