acts_as_sluggable 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,24 +13,36 @@ module Acts
13
13
  # The name of the field where the slug will be stored
14
14
  #
15
15
  def acts_as_sluggable(options = {})
16
- options = {
16
+ @acts_as_sluggable_options = {
17
17
  :generate_from => :name,
18
18
  :store_as => :slug
19
19
  }.merge(options)
20
20
 
21
+ generate_from = @acts_as_sluggable_options[:generate_from]
22
+ store_as = @acts_as_sluggable_options[:store_as]
23
+
21
24
  class_eval do
22
25
  before_save do
23
- generate_slug(options[:generate_from], options[:store_as])
26
+ generate_slug(generate_from, store_as)
24
27
  end
25
- field(options[:store_as], :type => String) unless self.respond_to?(options[:store_as])
26
- index(options[:store_as])
28
+
29
+ field(store_as, :type => String) unless self.respond_to?(store_as)
30
+ index(store_as)
27
31
  alias_method :to_param!, :to_param
28
32
  end
29
33
 
30
34
  define_method("to_param") do
31
- self.send(options[:store_as])
35
+ self.send(store_as)
32
36
  end
33
37
  end
38
+
39
+ def acts_as_sluggable_options
40
+ @acts_as_sluggable_options
41
+ end
42
+
43
+ def find_by_slug(slug)
44
+ where(@acts_as_sluggable_options[:store_as] => slug).first
45
+ end
34
46
  end
35
47
 
36
48
  module InstanceMethods
@@ -1,5 +1,5 @@
1
1
  module Acts
2
2
  module Sluggable
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -41,6 +41,12 @@ class TestActsAsSluggable < Test::Unit::TestCase
41
41
  assert_equal('some-name', @project.to_param)
42
42
  end
43
43
  end
44
+
45
+ context :find_by_slug do
46
+ should "find the object" do
47
+ assert_equal(@project, Project.find_by_slug(@project.to_param))
48
+ end
49
+ end
44
50
  end
45
51
 
46
52
  context "custom parameters" do
@@ -57,6 +63,23 @@ class TestActsAsSluggable < Test::Unit::TestCase
57
63
  assert_equal('some-other-name', @organization.to_param)
58
64
  end
59
65
  end
66
+
67
+ context :find_by_slug do
68
+ should "find the object" do
69
+ assert_equal(@organization, Organization.find_by_slug(@organization.to_param))
70
+ end
71
+ end
72
+ end
73
+
74
+ context :acts_as_sluggable_options do
75
+ should "respond to acts_as_sluggable_options" do
76
+ assert_equal(true, Project.respond_to?(:acts_as_sluggable_options))
77
+ end
78
+
79
+ should "return the options for acts_as_sluggable" do
80
+ assert_equal(:name, Project.acts_as_sluggable_options[:generate_from])
81
+ assert_equal(:slug, Project.acts_as_sluggable_options[:store_as])
82
+ end
60
83
  end
61
84
 
62
85
  def teardown
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_sluggable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Padilla
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-27 00:00:00 -05:00
18
+ date: 2010-10-28 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency