mongoid_taggable_on 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +10 -0
  2. data/lib/mongoid/taggable_on.rb +66 -3
  3. metadata +5 -5
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Mongoid Taggable provides some helpers to create taggable documents, can use many fields.
4
4
 
5
+ ## Status
6
+
7
+ [![CI Status](https://secure.travis-ci.org/huacnlee/mongoid_taggable_on.png)](http://travis-ci.org/huacnlee/mongoid_taggable_on)
8
+
5
9
  ## Installation
6
10
 
7
11
  You can simple install from rubygems:
@@ -36,6 +40,12 @@ Now you can use sample:
36
40
  irb> m.countries
37
41
  ["United States","China","Mexico"]
38
42
 
43
+ find with tag:
44
+
45
+ irb> Movie.tagged_with_on(:actors, "Jason Statham, Joseph Gordon-Levitt")
46
+ irb> Movie.tagged_with_on(:actors, "Jason Statham, Joseph Gordon-Levitt", :match => :any)
47
+ irb> Movie.tagged_with_on(:actors, "Nicolas Cage", :match => :not)
48
+
39
49
  ## Allow split chars
40
50
 
41
51
  , ,| /
@@ -3,11 +3,70 @@ module Mongoid
3
3
  module TaggableOn
4
4
  extend ActiveSupport::Concern
5
5
 
6
- def self.included(base)
7
- base.extend(ClassMethods)
6
+ included do
7
+ =begin rdoc
8
+ find items with tag
9
+
10
+ *Params*
11
+
12
+ * field_name tagged field
13
+ * tags match value, allow String or Array, case insensitive, for example: "Ruby,Python" or ["Ruby","Python"] or "ruby"
14
+ * match (any,all,not) match type, default: all
15
+
16
+ *Usage*
17
+
18
+ Person.tagged_with_on(:skills, "ui design, photograph")
19
+ Person.tagged_with_on(:skills, "ui design, photograph").tagged_with_on(:languages, "english,chinese")
20
+ Person.tagged_with_on(:skills, "ui design, photograph").paginate(:page => params[:page])
21
+
22
+ =end
23
+ scope :tagged_with_on, Proc.new { |field_name, tags, opts|
24
+ return [] if field_name.blank?
25
+ opts ||= {}
26
+ opts[:match] ||= ""
27
+ field_name = field_name.to_s.tableize
28
+ tags = split_tag(tags) if tags.is_a?(String)
29
+ tags = tags.collect { |t| /#{t}/i }
30
+ case opts[:match].to_sym
31
+ when :any then
32
+ any_in(:"#{field_name}" => tags)
33
+ when :not then
34
+ not_in(:"#{field_name}" => tags)
35
+ else
36
+ # ALL
37
+ all_in(:"#{field_name}" => tags)
38
+ end
39
+ }
8
40
  end
9
41
 
10
42
  module ClassMethods
43
+
44
+ =begin rdoc
45
+ =Define a tag field
46
+ *For example:*
47
+
48
+ class Person
49
+ include Mongoid::Document
50
+ include Mongoid::TaggableOn
51
+ taggable_on :languages
52
+ taggable_on :skills
53
+ end
54
+
55
+ *Then will has there methods and fields:*
56
+
57
+ field :languages, :type => Array, :default => []
58
+ def language_list; end
59
+ def language_list=(value); end
60
+ field :skills, :type => Array, :default => []
61
+ def skill_list; end
62
+ def skill_list=(value); end
63
+
64
+ *Params*
65
+
66
+ * field_name <em>name for tag field</em>
67
+ * opts[:index] <em>(true/false) allow create index in MongoDB, default: true</em>
68
+
69
+ =end
11
70
  def taggable_on(field_name, opts = {})
12
71
  field_name = field_name.to_s.tableize
13
72
  field_name_single = field_name.singularize
@@ -24,7 +83,7 @@ module Mongoid
24
83
 
25
84
  def #{field_name_single}_list=(value)
26
85
  if !value.blank?
27
- self.#{field_name} = value.split(/,|,|\\/|\\|/).collect { |tag| tag.strip }.uniq
86
+ self.#{field_name} = self.class.split_tag(value)
28
87
  end
29
88
  end
30
89
 
@@ -34,6 +93,10 @@ module Mongoid
34
93
  end
35
94
  }
36
95
  end
96
+
97
+ def split_tag(value)
98
+ value.split(/,|,|\/|\|/).collect { |tag| tag.strip }.uniq
99
+ end
37
100
  end
38
101
  end
39
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_taggable_on
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-03-21 00:00:00.000000000 Z
12
+ date: 2012-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &70127184176480 !ruby/object:Gem::Requirement
16
+ requirement: &70160600877500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>'
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 2.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70127184176480
24
+ version_requirements: *70160600877500
25
25
  description: Mongoid Taggable provides some helpers to create taggable documents,
26
26
  can use many fields.
27
27
  email:
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  requirements: []
55
55
  rubyforge_project:
56
- rubygems_version: 1.8.11
56
+ rubygems_version: 1.8.10
57
57
  signing_key:
58
58
  specification_version: 3
59
59
  summary: Mongoid Taggable provides some helpers to create taggable documents, can