public
Description: something similiar to acts_as_taggable_on_steroids for datamapper
Homepage: http://aaron.aaron033.com
Clone URL: git://github.com/aq1018/dm-is-taggable.git
dm-is-taggable / README.txt
100644 188 lines (130 sloc) 3.907 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Please go to http://github.com/aq1018/dm-is-taggable/tree/master/README.textile for a pretty readme
 
h1. dm-is-taggable
 
dm-is-taggable is a tagging system built for datamapper. It has supports for multiple tagger types and taggable types.
Each tagger can tag different taggable objects.
 
 
h2. Installation
 
h3. Download the plugin
 
In your console:
<pre><code>
git clone git://github.com/aq1018/dm-is-taggable.git
</code></pre>
 
h3. Install the gem
 
In your console:
<pre><code>
cd dm-is-taggable
sudo rake install
</code></pre>
 
h3. Include it Merb
 
In merb init.rb:
 
<pre><code>
dependency "dm-is-taggable"
</code></pre>
 
 
h2. Using dm-is-taggable in your code
 
h3. Define taggers
 
<pre><code>
  class User
    include DataMapper::Resource
    property :id, Serial
    property :name, String
    is :tagger, :on => ["Article", "Picture"]
  end
 
  class Bot
    include DataMapper::Resource
    property :id, Serial
    property :name, String
    is :tagger, :on => ["Article", "Picture"]
  end
</code></pre>
 
h3. Define taggables
 
<pre><code>
  class Article
    include DataMapper::Resource
    property :id, Serial
    is :taggable, :by => ["User", "Bot"]
  end
 
  class Picture
    include DataMapper::Resource
    property :id, Serial
    is :taggable, :by => ["User", "Bot"]
  end
</code></pre>
 
h3. Create tags
 
<pre><code>
  
  @picture = Picture.first
  @scott = User.first
  
  # You can tag like this
  @picture.tag(:with => "shanghai, bar, beer", :by => @scott)
  
  # or like this
  # Note: this doesn't remove the previous tags
  Tag.as(@scott) do
    @picture.tag(:with => "cool, tag1, tag2")
  end
  
  # or like this
  # Note, this removes all previous tags
  Tag.as(@scott) do
    @picture.taglist("cool, tag1, tag2")
  end
</code></pre>
 
h3. Retrieve objects with tags
 
<pre><code>
  
  # find pictures tagged with tag1 and tag2
  Picture.find(:with => "tag1, tag2")
  
  # find pictures tagged with tag1 or tag2
  Picture.find(:with => "tag1, tag2", :match => :any)
  
  # find pictures tagged with tag1 or tag2, tagged by @user1
  Picture.find(:with => "tag1, tag2", :match => :any, :by => @user1)
  
  # find pictures tagged with tag1 or tag2, tagged by all users
  Picture.find(:with => "tag1, tag2", :match => :any, :by => User)
  
  # or you can do scoped way
  
  # find pictures tagged with tag1 or tag2, tagged by all users
  Tag.as(User) do
    Picture.find(:with => "tag1, tag2", :match => :any)
  end
  
  # find pictures tagged with tag1 or tag2, tagged by @user1
  Tag.as(@user1) do
    Picture.find(:with => "tag1, tag2", :match => :any)
  end
  
    
  # You can tag like this
  @picture.tag(:with => "shanghai, bar, beer", :by => @scott)
  
  # or like this
  # Note: this doesn't remove the previous tags
  Tag.as(@scott) do
    @picture.tag(:with => "cool, tag1, tag2")
  end
  
  # or like this
  # Note, this removes all previous tags
  Tag.as(@scott) do
    @picture.taglist("cool, tag1, tag2")
  end
</code></pre>
 
h3. Retrieve tags with objects
 
<pre><code>
  
  @picture1 = Picture.first
  
  # get all tags associated with @picture2 as a string
  @picture1.taglist
  
  # or as tag objects
  @picture1.tags
  
  # tags tagged by users
  @picture1.tags_by_users
  
  # find tags by all users
  Tag.by(User)
 
  # find tags by a user
  Tag.by(@user)
 
  # find tags on all pictures
  Tag.on(Picture)
  
  # find tags on a picture
  Tag.on(@picture1)
  
  # find tags by a user, on all pictures
  Tag.by(@user).on(Pictures)
  
  # find tags by all users on a picture
  Tag.by(User).on(@picture)
  
  # find tags by a user on a picture
  Tag.by(@user).on(@picture)
  
</code></pre>
 
h3. Counting tags
 
<pre><code>
  
  # Count how many articles are tagged by @user1
  Tag.tagged_count(:by => @user1, :on => Article)
  
  # Count how many articles are tagged by @user1 with "tag1"
  Tag.tagged_count(:by => @user1, :on => Article, :with => "tag1")
    
</code></pre>