rocket_tag 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -62,37 +62,55 @@ module RocketTag
62
62
  @rocket_tag ||= RocketTag::Taggable::Manager.new(self)
63
63
  end
64
64
 
65
- def with_tag_context context
65
+ def _with_tag_context context
66
66
  if context
67
67
  where{taggings.context == my{context} }
68
68
  else
69
- where{}
69
+ where{ }
70
70
  end
71
71
  end
72
72
 
73
- def tagged_with tags_list, options = {}
73
+ def _with_all condition, tags_list
74
+ if condition
75
+ group{~id}.
76
+ having{count(~id)==my{tags_list.length}}
77
+ else
78
+ where{}
79
+ end
80
+ end
74
81
 
82
+ # Generates a sifter or a where clause depending on options.
83
+ # The sifter generates a subselect with the body of the
84
+ # clause wrapped up so that it can be used as a condition
85
+ # within another squeel statement.
86
+ #
87
+ # Query optimization is left up to the SQL engine.
88
+ def tagged_with_sifter tags_list, options = {}
75
89
  on = options.delete :on
76
90
  all = options.delete :all
77
91
 
78
- q = if all
79
- joins{tags}.where{
80
- id.in(
81
- my{self}.
82
- select{id}.
83
- joins{tags}.
84
- where{tags.name.in(my{tags_list})}.
85
- group{~id}.
86
- having{count(~id)==my{tags_list.length}}.
87
- with_tag_context(my{on})
88
- )
89
- }
90
- else
91
- joins{tags}.where{tags.name.in(my{tags_list})}.with_tag_context(on)
92
- end
92
+ proc do |&block|
93
+ if options.delete :where
94
+ where &block
95
+ else
96
+ squeel &block
97
+ end
98
+ end.call {
99
+ id.in(
100
+ my{self}.
101
+ select{id}.
102
+ joins{tags}.
103
+ where{ tags.name.in(my{tags_list})}.
104
+ _with_tag_context(my{on}).
105
+ _with_all(my{all}, my{tags_list})
106
+ )
107
+ }
93
108
 
94
- q.select{"distinct #{my{table_name}}.*"}
109
+ end
95
110
 
111
+ def tagged_with tags_list, options = {}
112
+ options[:where] = true
113
+ tagged_with_sifter(tags_list, options)
96
114
  end
97
115
 
98
116
  def attr_taggable *contexts
data/rocket_tag.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rocket_tag}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Brad Phelan}]
12
- s.date = %q{2011-09-16}
12
+ s.date = %q{2011-09-19}
13
13
  s.description = %q{}
14
14
  s.email = %q{bradphelan@xtargets.com}
15
15
  s.extra_rdoc_files = [
@@ -56,55 +56,6 @@ describe TaggableModel do
56
56
  end
57
57
  end
58
58
 
59
-
60
- it "allows me to get funky with Squeel and ActiveRelation" do
61
-
62
- sql = <<-EOF.gsub(/\s+/, ' ').strip
63
- SELECT "taggable_models".*
64
- FROM "taggable_models"
65
- INNER JOIN "taggings"
66
- ON "taggings"."taggable_id" = "taggable_models"."id"
67
- AND "taggings"."taggable_type" = 'TaggableModel'
68
- INNER JOIN "tags"
69
- ON "tags"."id" = "taggings"."tag_id"
70
- AND taggings.context = 'skills'
71
- WHERE
72
- "tags"."name" = 'foo'
73
- EOF
74
-
75
- TaggableModel.joins{skills_tags}.where{skills_tags.name == "foo"}.to_sql.should == sql
76
-
77
- sql = <<-EOF.gsub(/\s+/, ' ').strip
78
- SELECT distinct taggable_models.*
79
- FROM "taggable_models"
80
- INNER JOIN "taggings"
81
- ON
82
- "taggings"."taggable_id" = "taggable_models"."id"
83
- AND
84
- "taggings"."taggable_type" = 'TaggableModel'
85
- INNER JOIN "tags"
86
- ON
87
- "tags"."id" = "taggings"."tag_id"
88
- WHERE
89
- "taggable_models"."id" IN
90
- (SELECT "taggable_models"."id"
91
- FROM "taggable_models"
92
- INNER JOIN "taggings"
93
- ON "taggings"."taggable_id" = "taggable_models"."id"
94
- AND "taggings"."taggable_type" = 'TaggableModel'
95
- INNER JOIN "tags"
96
- ON "tags"."id" = "taggings"."tag_id" WHERE "tags"."name"
97
- IN ('a', 'b')
98
- GROUP BY "taggable_models"."id"
99
- HAVING count("taggable_models"."id") = 2)
100
- AND
101
- (created_at > '2011-09-16 05:41')
102
- EOF
103
-
104
- TaggableModel.tagged_with(["a", "b"], :all =>true).where(["created_at > ?", "2011-09-16 05:41"]).to_sql.should == sql
105
- end
106
-
107
-
108
59
  describe "combining with active relation" do
109
60
  before :each do
110
61
  TaggableModel.create :name => "test 0", :needs => %w[x y z]
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rocket_tag
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Brad Phelan
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-16 00:00:00 Z
13
+ date: 2011-09-19 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  requirements:
144
144
  - - ">="
145
145
  - !ruby/object:Gem::Version
146
- hash: -3759555627206033387
146
+ hash: -1469878759169325224
147
147
  segments:
148
148
  - 0
149
149
  version: "0"