tagalong 0.0.5 → 0.0.6

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.
data/README.md CHANGED
@@ -94,15 +94,30 @@ To list Tagger tags, call the `tags` method on a Tagger object. This will return
94
94
  @user.tags
95
95
  # => ['another_tag', 'some_tag', 'woot_tag']
96
96
 
97
- ### List Tagger tags with usage info
97
+ ### List Tagger tags with more information
98
98
 
99
- To list Tagger tags with usage info, call the `tags` method on a Tagger object passing in a Taggable object. This will return a list of hash objects containing the tag ( **:tag** ), a boolean representing if the Taggable passed in is currently tagged with that tag ( **:used** ), and the number of references of that tag by the Tagger ( **:number_of_references** ). The resulting list of hashes is ordered by tag in ascending alphabetical order.
99
+ You can get a list of tags that belong to a Tagger along with some additional information by calling the `tags_including` method. The result will be a hash that always contains the key **:name** which is the name of the tag. There can be other keys added to the hash if you choose to pass the option for them to the method. A list of the params you can pass and their explanations are below:
100
100
 
101
- @user.tags(@contact)
101
+ ##### :number_of_references => true
102
+
103
+ This only takes true as a value. When passed, it will add **:number_of_references** to the result hash. The value of **:number_of_references** is the number of times the tag is used by the Tagger.
104
+
105
+ @user.tags_including(:number_of_references => true)
106
+ # => [
107
+ { name: 'another_tag', :number_of_references => 42 },
108
+ { name: 'some_tag', :number_of_references => 23 },
109
+ { name: 'woot_tag', :number_of_references => 2 }
110
+ ]
111
+
112
+ ##### :has_been_tagged => @contact
113
+
114
+ Takes a Taggable as a value. When passed, it will add **:has_been_tagged** to the result hash for each tag returned. The value of **:has_been_tagged** will contain a boolean representing if the Taggable passed is tagged by the current tag in the iteration.
115
+
116
+ @user.tags_including(:has_been_tagged => @contact)
102
117
  # => [
103
- { tag: 'another_tag', :used => false, :number_of_references => 42 },
104
- { tag: 'some_tag', :used => true, :number_of_references => 23 },
105
- { tag: 'woot_tag', :used => true, :number_of_references => 2 }
118
+ { name: 'another_tag', :has_been_tagged => true },
119
+ { name: 'some_tag', :has_been_tagged => false },
120
+ { name: 'woot_tag', :has_been_tagged => true }
106
121
  ]
107
122
 
108
123
  ### List Taggable tags
@@ -33,15 +33,32 @@ module Tagalong
33
33
  end
34
34
  end
35
35
 
36
- def tags(taggable = nil)
37
- if taggable == nil
38
- return self.tagalong_tags.order("name ASC").map { |r| r.name }
39
- else
40
- return self.tagalong_tags.
41
- joins("LEFT OUTER JOIN tagalong_taggings ON tagalong_taggings.tagalong_tag_id = tagalong_tags.id AND tagalong_taggings.taggable_id = '#{taggable.id.to_s}'").
42
- select("tagalong_tags.id, tagalong_tags.name, tagalong_tags.number_of_references, tagalong_taggings.id as used").
43
- order("name ASC").map { |r| { :tag => r.name, :used => !r.used.nil?, :number_of_references => r.number_of_references } }
36
+ def tags
37
+ self.tagalong_tags.order("name ASC").map { |r| r.name }
38
+ end
39
+
40
+ def tags_including(options={})
41
+ out = []
42
+ query = self.tagalong_tags.order("name ASC")
43
+
44
+ if options[:has_been_tagged]
45
+ query = query.
46
+ select("tagalong_tags.id, tagalong_tags.name, tagalong_tags.number_of_references, tagalong_taggings.id as used").
47
+ joins("LEFT OUTER JOIN tagalong_taggings ON tagalong_taggings.tagalong_tag_id = tagalong_tags.id AND tagalong_taggings.taggable_id = '#{options[:has_been_tagged].id.to_s}'")
48
+ end
49
+
50
+ query.each do |tag|
51
+ hash = {:name => tag.name}
52
+ if options[:number_of_references]
53
+ hash[:number_of_references] = tag.number_of_references
54
+ end
55
+ if options[:has_been_tagged]
56
+ hash[:has_been_tagged] = !tag.used.nil?
57
+ end
58
+ out << hash
44
59
  end
60
+
61
+ return out
45
62
  end
46
63
 
47
64
  def taggables_with(name)
@@ -1,3 +1,3 @@
1
1
  module Tagalong
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -63,43 +63,40 @@ describe "Tagger" do
63
63
  @user.tags.should == ["bar", "car", "foo"]
64
64
  end
65
65
  end
66
+ end
67
+
68
+ describe "#tags_including" do
69
+ before(:each) do
70
+ @user.tag(@contact, "tag1")
71
+ @user.tag(@contact, "tag2")
72
+ end
66
73
 
67
- context "with a taggable" do
68
- it "returns a hash of the tags with usage information about the passed taggable" do
69
- tag = @user.tagalong_tags.create!(:name => "foo", :number_of_references => 1)
70
- @contact.tagalong_taggings.create!(:tagalong_tag_id => tag.id)
71
- @user.tagalong_tags.create!(:name => "bar", :number_of_references => 0)
72
- tag = @user.tagalong_tags.create!(:name => "car", :number_of_references => 1)
73
- @contact.tagalong_taggings.create!(:tagalong_tag_id => tag.id)
74
- @user.tags(@contact).should == [
75
- { :tag => "bar", :used => false, :number_of_references => 0 },
76
- { :tag => "car", :used => true, :number_of_references => 1 },
77
- { :tag => "foo", :used => true, :number_of_references => 1 }
74
+ context "without options passed" do
75
+ it "should return an array of hashes with name" do
76
+ @user.tags_including.should == [
77
+ {:name => 'tag1'},
78
+ {:name => 'tag2'}
78
79
  ]
79
80
  end
80
-
81
- it "returns a hash of tags with usage information about the passed taggable on secondary calls when the taggable changes" do
82
- tag = @user.tagalong_tags.create!(:name => "foo", :number_of_references => 1)
83
- @contact.tagalong_taggings.create!(:tagalong_tag_id => tag.id)
84
- @user.tagalong_tags.create!(:name => "bar", :number_of_references => 0)
85
- tag = @user.tagalong_tags.create!(:name => "car", :number_of_references => 1)
86
- @contact.tagalong_taggings.create!(:tagalong_tag_id => tag.id)
87
- @user.tags(@contact).should == [
88
- { :tag => "bar", :used => false, :number_of_references => 0 },
89
- { :tag => "car", :used => true, :number_of_references => 1 },
90
- { :tag => "foo", :used => true, :number_of_references => 1 }
81
+ end
82
+
83
+ context "with number_of_references passed" do
84
+ it "should return an array of hashes with name and number_of_references" do
85
+ @user.tags_including(:number_of_references => true).should == [
86
+ {:name => 'tag1', :number_of_references => 1},
87
+ {:name => 'tag2', :number_of_references => 1}
91
88
  ]
92
- @other_contact = Contact.create!(:name => "My Other Taggable")
93
- tag = @user.tagalong_tags.create!(:name => "jones", :number_of_references => 1)
94
- @other_contact.tagalong_taggings.create!(:tagalong_tag_id => tag.id)
95
- tag = @user.tagalong_tags.create!(:name => "jimmy", :number_of_references => 2)
96
- @other_contact.tagalong_taggings.create!(:tagalong_tag_id => tag.id)
97
- @user.tags(@other_contact).should == [
98
- { :tag => "bar", :used => false, :number_of_references => 0 },
99
- { :tag => "car", :used => false, :number_of_references => 1 },
100
- { :tag => "foo", :used => false, :number_of_references => 1 },
101
- { :tag => "jimmy", :used => true, :number_of_references => 2 },
102
- { :tag => "jones", :used => true, :number_of_references => 1 }
89
+ end
90
+ end
91
+
92
+ context "with a valid taggable passed as has_been_tagged" do
93
+ it "should return an array of hashes with name and has_been_tagged" do
94
+ @contact2 = Contact.create!(:name => "My Taggable 2")
95
+ @user.tag(@contact2, 'tag3')
96
+ @user.tags_including(:has_been_tagged => @contact).should == [
97
+ {:name => 'tag1', :has_been_tagged => true},
98
+ {:name => 'tag2', :has_been_tagged => true},
99
+ {:name => 'tag3', :has_been_tagged => false}
103
100
  ]
104
101
  end
105
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagalong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000 Z
12
+ date: 2012-05-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70195968616540 !ruby/object:Gem::Requirement
16
+ requirement: &70298657761420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70195968616540
24
+ version_requirements: *70298657761420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &70195968616100 !ruby/object:Gem::Requirement
27
+ requirement: &70298657760980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70195968616100
35
+ version_requirements: *70298657760980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70195968615680 !ruby/object:Gem::Requirement
38
+ requirement: &70298657760560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70195968615680
46
+ version_requirements: *70298657760560
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70195968615240 !ruby/object:Gem::Requirement
49
+ requirement: &70298657760120 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70195968615240
57
+ version_requirements: *70298657760120
58
58
  description: A Rails tagging plugin that makes sense.
59
59
  email:
60
60
  - cyphactor@gmail.com