relata 0.0.3 → 0.0.4
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.markdown +30 -22
- data/Rakefile +3 -3
- data/init.rb +1 -1
- data/lib/relata.rb +3 -0
- data/lib/relata/dsl.rb +1 -0
- data/lib/relata/dsl/conditions.rb +21 -1
- data/lib/relata/dsl/constraints.rb +19 -6
- data/lib/relata/dsl/custom_relation.rb +21 -1
- data/lib/relata/dsl/field_search.rb +30 -4
- data/lib/relata/dsl/querys/belongs.rb +8 -0
- data/lib/relata/dsl/querys/fields.rb +9 -4
- data/lib/relata/filter.rb +21 -1
- data/test.sqlite3 +0 -0
- data/test/dsl_test.rb +25 -31
- data/test/filtered_relation_test.rb +40 -40
- data/test/relation_dsl_test.rb +57 -0
- data/test/schema.rb +14 -0
- metadata +8 -7
- data/lib/filtered_relation.rb +0 -3
- data/test/test.sqlite3 +0 -0
data/README.markdown
CHANGED
@@ -1,27 +1,10 @@
|
|
1
|
-
#
|
1
|
+
#Relata#
|
2
2
|
|
3
|
-
|
3
|
+
Helps poking around with relationships when using ARel.
|
4
4
|
|
5
|
-
##
|
6
|
-
|
7
|
-
Create dynamic filters with just onde method.
|
8
|
-
|
9
|
-
<% form_tag :action => 'filter' do %>
|
10
|
-
Title: <%= text_field_tag 'post[title]' %><br />
|
11
|
-
Only with Comments?
|
12
|
-
<%= select("post", "comments", options_for_select({ "false" => "", "true" => "true" })) %> %>
|
13
|
-
|
14
|
-
def filter
|
15
|
-
@posts = Post.filtered_relation(params[:post]).all
|
16
|
-
end
|
17
|
-
|
18
|
-
Create more advanced relations.
|
19
|
-
|
20
|
-
posts = Post.filtered_relation(:comments => true).where(:user_id => 4).limit(3).order("id ASC")
|
5
|
+
## Install ##
|
21
6
|
|
22
|
-
|
23
|
-
# records
|
24
|
-
end
|
7
|
+
gem install
|
25
8
|
|
26
9
|
## DSL API ##
|
27
10
|
|
@@ -40,4 +23,29 @@ Create more advanced relations.
|
|
40
23
|
Post.where { comments >= 2 }
|
41
24
|
|
42
25
|
Post.where { published_at.between(2.years.ago, 6.months.ago) }
|
43
|
-
|
26
|
+
|
27
|
+
Post.where(:user).name.like?("anderson")
|
28
|
+
|
29
|
+
Post.where(:comments).description.like?("%dsl test%")
|
30
|
+
|
31
|
+
|
32
|
+
## Filter Example ##
|
33
|
+
|
34
|
+
Create dynamic filters with just onde method.
|
35
|
+
|
36
|
+
<% form_tag :action => 'filter' do %>
|
37
|
+
Title: <%= text_field_tag 'post[title]' %><br />
|
38
|
+
Only with Comments?
|
39
|
+
<%= select("post", "comments", options_for_select({ "false" => "", "true" => "true" })) %> %>
|
40
|
+
|
41
|
+
def filter
|
42
|
+
@posts = Post.filtered_relation(params[:post]).all
|
43
|
+
end
|
44
|
+
|
45
|
+
Create more advanced relations.
|
46
|
+
|
47
|
+
posts = Post.filtered_relation(:comments => true).where(:user_id => 4).limit(3).order("id ASC")
|
48
|
+
|
49
|
+
posts.each do |post|
|
50
|
+
# records
|
51
|
+
end
|
data/Rakefile
CHANGED
@@ -17,7 +17,7 @@ end
|
|
17
17
|
desc 'Generate documentation for the relata plugin.'
|
18
18
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
19
19
|
rdoc.rdoc_dir = 'rdoc'
|
20
|
-
rdoc.title = '
|
20
|
+
rdoc.title = 'Relata'
|
21
21
|
rdoc.options << '--line-numbers' << '--inline-source'
|
22
22
|
rdoc.rdoc_files.include('README.markdown')
|
23
23
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
@@ -27,8 +27,8 @@ PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*',
|
|
27
27
|
|
28
28
|
spec = Gem::Specification.new do |s|
|
29
29
|
s.name = "relata"
|
30
|
-
s.version = "0.0.
|
31
|
-
s.author = "Anderson Leite, Guilherme Silveira"
|
30
|
+
s.version = "0.0.4"
|
31
|
+
s.author = "Anderson Leite, Guilherme Silveira, Pedro Mariano"
|
32
32
|
s.email = "anderson.leite@caelum.com.br"
|
33
33
|
s.homepage = "http://github.com/caelum/relata"
|
34
34
|
s.platform = Gem::Platform::RUBY
|
data/init.rb
CHANGED
data/lib/relata.rb
ADDED
data/lib/relata/dsl.rb
CHANGED
@@ -1,40 +1,60 @@
|
|
1
|
+
# = Conditions
|
1
2
|
# A custom set of conditions that can be applied
|
2
3
|
# to a query
|
3
4
|
module Relata::Dsl::Conditions
|
4
5
|
|
6
|
+
# Return all objects whith exactly X associated objects
|
7
|
+
# Post.where(:comments).count.eq(2)
|
5
8
|
def eq(value)
|
6
9
|
add_filter("= #{value}")
|
7
10
|
end
|
8
11
|
|
12
|
+
# Return all objects whith more or exactly X associated objects
|
13
|
+
# Post.where(:comments).count.ge(2)
|
9
14
|
def ge(value)
|
10
15
|
add_filter(">= #{value}")
|
11
16
|
end
|
12
17
|
|
18
|
+
# Return all objects whith more or exactly X associated objects
|
19
|
+
# Post.where(:comments).count.greater_or_equals(2)
|
13
20
|
def greater_or_equals(value)
|
14
21
|
ge(value)
|
15
22
|
end
|
16
23
|
|
24
|
+
# Return all objects whith more than X associated objects
|
25
|
+
# Post.where(:comments).count.gt(2)
|
17
26
|
def gt(value)
|
18
27
|
add_filter("> #{value}")
|
19
28
|
end
|
20
29
|
|
30
|
+
# Return all objects whith more than X associated objects
|
31
|
+
# Alias for "gt"
|
32
|
+
# Post.where(:comments).count.greater_than(2)
|
21
33
|
def greater_than(value)
|
22
34
|
gt(value)
|
23
35
|
end
|
24
36
|
|
37
|
+
# Return all objects whith less or equals X associated objects
|
38
|
+
# Post.where(:comments).count.le(2)
|
25
39
|
def le(value)
|
26
40
|
add_filter("<= #{value}")
|
27
41
|
end
|
28
42
|
|
43
|
+
# Return all objects whith less than X associated objects
|
44
|
+
# Post.where(:comments).count.lt(2)
|
29
45
|
def lt(value)
|
30
46
|
add_filter("< #{value}")
|
31
47
|
end
|
32
48
|
|
49
|
+
# Return all objects whith less than X associated object
|
50
|
+
# Alias for "lt"
|
51
|
+
# Post.where(:comments).count.lesser_than(2)
|
33
52
|
def lesser_than(value)
|
34
53
|
lt value
|
35
54
|
end
|
36
55
|
|
37
|
-
#
|
56
|
+
# Whether this relation has at least one element.
|
57
|
+
# Post.where(:comments).exists?
|
38
58
|
def exists?
|
39
59
|
if @relation_search.nil?
|
40
60
|
count.exists?
|
@@ -1,16 +1,15 @@
|
|
1
|
+
# = Constraints
|
2
|
+
# A custom set of constraints that can be applied
|
3
|
+
# to a query
|
1
4
|
module Relata::Dsl::Constraints
|
2
5
|
|
6
|
+
# Return all objects which fields by size
|
7
|
+
# Post.where { body.length < 22 }
|
3
8
|
def length
|
4
9
|
@relation_search = LengthManager
|
5
10
|
self
|
6
11
|
end
|
7
12
|
|
8
|
-
class LengthManager
|
9
|
-
def self.condition(field, *args)
|
10
|
-
"len(field)"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
13
|
def count
|
15
14
|
@select_fields << "COUNT(#{@current_field}.id) AS count"
|
16
15
|
@groups << "#{table_name}.id"
|
@@ -18,9 +17,17 @@ module Relata::Dsl::Constraints
|
|
18
17
|
self
|
19
18
|
end
|
20
19
|
|
20
|
+
# Return all objects which match with parameter
|
21
|
+
# Post.where(:body).like?("%caelum%")
|
21
22
|
def like?(value)
|
22
23
|
query.where("#{@current_field} like ?", [value])
|
23
24
|
end
|
25
|
+
|
26
|
+
# Return all objects whith nil value fields
|
27
|
+
# Post.where(:body).is_null
|
28
|
+
def is_null
|
29
|
+
query.where("#{@current_field} is NULL")
|
30
|
+
end
|
24
31
|
|
25
32
|
# def between(first, second)
|
26
33
|
# @relation_search = SimpleRangeCondition
|
@@ -29,6 +36,12 @@ module Relata::Dsl::Constraints
|
|
29
36
|
# # add_filter("> #{first}").add_filter("< #{second}")
|
30
37
|
# end
|
31
38
|
|
39
|
+
class LengthManager
|
40
|
+
def self.condition(field, *args)
|
41
|
+
"len(field)"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
32
45
|
class SimpleCondition
|
33
46
|
def self.condition(field, *args)
|
34
47
|
"#{field}"
|
@@ -9,9 +9,13 @@ module Relata::Dsl::CustomRelation
|
|
9
9
|
@start_field = field
|
10
10
|
@select_fields = ["#{table_name}.*"]
|
11
11
|
@groups = []
|
12
|
+
|
12
13
|
if relates_to_many?
|
13
14
|
self.extend MultipleQuery
|
14
15
|
self.extend ModelFields
|
16
|
+
elsif relates_belongs_to?
|
17
|
+
self.extend BelongsToQuery
|
18
|
+
self.extend ModelFields
|
15
19
|
else
|
16
20
|
self.extend SimpleQuery
|
17
21
|
end
|
@@ -19,8 +23,24 @@ module Relata::Dsl::CustomRelation
|
|
19
23
|
self
|
20
24
|
end
|
21
25
|
|
26
|
+
|
27
|
+
|
22
28
|
def relates_to_many?
|
23
|
-
|
29
|
+
check_relation "has_many"
|
30
|
+
end
|
31
|
+
|
32
|
+
def relates_belongs_to?
|
33
|
+
check_relation "belongs_to"
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def check_relation type
|
38
|
+
@record.reflect_on_all_associations.each do |r|
|
39
|
+
if r.name.to_sym.eql? @current_field.to_sym
|
40
|
+
return true if r.macro.to_s.eql? type
|
41
|
+
end
|
42
|
+
end
|
43
|
+
false
|
24
44
|
end
|
25
45
|
|
26
46
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# = Field Search
|
1
2
|
# a relation search in a specific field
|
2
3
|
class Relata::Dsl::FieldSearch
|
3
4
|
|
@@ -6,39 +7,64 @@ class Relata::Dsl::FieldSearch
|
|
6
7
|
@field = field
|
7
8
|
end
|
8
9
|
|
10
|
+
# Exactly number of relation or field value
|
11
|
+
# Post.where { body == "CaelumObjects training and inovation" }
|
12
|
+
# Post.where { comments == 2 }
|
9
13
|
def ==(value)
|
10
14
|
@rel.where("#{@field} == ?", value)
|
11
15
|
end
|
12
16
|
|
17
|
+
# All records with different field value
|
18
|
+
# Post.where { comments != 2 }
|
19
|
+
# def !=(value)
|
20
|
+
# @rel.where("#{@field} <> ?", value)
|
21
|
+
# end
|
22
|
+
|
23
|
+
# All records with higher or equal number of relations
|
24
|
+
# Post.where { comments >= 2 }
|
13
25
|
def >=(value)
|
14
26
|
@rel.where("#{@field} >= ?", value)
|
15
27
|
end
|
16
28
|
|
29
|
+
# All records with less or equal number of relations
|
30
|
+
# Post.where { comments <= 2 }
|
17
31
|
def <=(value)
|
18
32
|
@rel.where("#{@field} <= ?", value)
|
19
33
|
end
|
20
|
-
|
34
|
+
|
35
|
+
# All records with higher number of relations
|
36
|
+
# Post.where { comments > 2 }
|
21
37
|
def >(value)
|
22
38
|
@rel.where("#{@field} > ?", value)
|
23
39
|
end
|
24
|
-
|
40
|
+
|
41
|
+
# All records with lesser number of relations
|
42
|
+
# Post.where { comments < 2 }
|
25
43
|
def <(value)
|
26
44
|
@rel.where("#{@field} < ?", value)
|
27
45
|
end
|
28
46
|
|
47
|
+
# Find records by field value
|
48
|
+
# Post.where(:body).like?("%caelum%")
|
29
49
|
def like?(value)
|
30
50
|
@rel.where(@field).like?(value)
|
31
51
|
end
|
32
52
|
|
53
|
+
# Find record with date between
|
54
|
+
# Post.where { published_at.between(2.years.ago, 6.months.ago) }
|
33
55
|
def between(first, second)
|
34
56
|
@rel.where("#{@field} > ? and #{@field} < ?", first, second)
|
35
57
|
end
|
36
|
-
|
58
|
+
|
59
|
+
# Find records by size
|
60
|
+
# Post.where { body.length < 22 }
|
37
61
|
def length
|
38
62
|
@field = "length(#{@field})"
|
39
63
|
self
|
40
64
|
end
|
41
|
-
|
65
|
+
|
66
|
+
# Your custom relation
|
67
|
+
# Post.where { body "like ?", "%lum%" }
|
42
68
|
def custom(*args)
|
43
69
|
comparison = args.shift
|
44
70
|
@rel.where("#{@field} #{comparison}", args)
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module BelongsToQuery
|
2
|
+
def query
|
3
|
+
preload(@start_field).select(@select_fields.join ',').from("#{table_name}, #{@start_field.to_s.pluralize}").where("#{table_name}.#{@start_field}_id = #{@start_field.to_s.pluralize}.id")
|
4
|
+
end
|
5
|
+
def add_filter expectation
|
6
|
+
query.group(@groups.first).having("#{@relation_search} #{expectation}")
|
7
|
+
end
|
8
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module ModelFields
|
2
2
|
|
3
3
|
def self.extended(base)
|
4
|
-
|
4
|
+
@fields = []
|
5
5
|
base.reflect_on_all_associations.each do |r|
|
6
|
-
|
7
|
-
c.name if [:string, :text].include? c.type
|
6
|
+
r.klass.columns.each do |c|
|
7
|
+
@fields << c.name if [:string, :text].include? c.type
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
@fields.each do |field|
|
12
12
|
include_method field if field != nil
|
13
13
|
end
|
@@ -17,6 +17,11 @@ module ModelFields
|
|
17
17
|
private
|
18
18
|
def self.include_method(field)
|
19
19
|
define_method field do
|
20
|
+
|
21
|
+
@record.reflect_on_all_associations.each do |r|
|
22
|
+
@current_field = @current_field.to_s.pluralize if r.macro.to_s.eql? "belongs_to"
|
23
|
+
end
|
24
|
+
|
20
25
|
@current_field = "#{@current_field}.#{field}"
|
21
26
|
self
|
22
27
|
end
|
data/lib/relata/filter.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
|
-
require '
|
1
|
+
require 'relata/related_query_methods'
|
2
2
|
ActiveRecord::Base.send :include, RelatedQueryMethods
|
3
3
|
|
4
|
+
|
5
|
+
# = Filtered Relation
|
6
|
+
# Create dynamic filters with just onde method.
|
7
|
+
|
8
|
+
# <% form_tag :action => 'filter' do %>
|
9
|
+
# Title: <%= text_field_tag 'post[title]' %><br />
|
10
|
+
# Only with Comments?
|
11
|
+
# <%= select("post", "comments", options_for_select({ "false" => "", "true" => "true" })) %> %>
|
12
|
+
#
|
13
|
+
# def filter
|
14
|
+
# @posts = Post.filtered_relation(params[:post]).all
|
15
|
+
# end
|
16
|
+
|
17
|
+
# Create more advanced relations with DSL.
|
18
|
+
|
19
|
+
# posts = Post.filtered_relation(:comments => true).where(:user_id => 4).limit(3).order("id ASC")
|
20
|
+
|
21
|
+
# posts.each do |post|
|
22
|
+
# # records
|
23
|
+
# end
|
4
24
|
module FilteredRelation
|
5
25
|
|
6
26
|
class ::ActiveRecord::Base
|
data/test.sqlite3
CHANGED
Binary file
|
data/test/dsl_test.rb
CHANGED
@@ -14,7 +14,7 @@ class DSLTest < ActiveSupport::TestCase
|
|
14
14
|
posts = Post.where(:body).like?("%caelum%").all
|
15
15
|
assert_equal @caelum, posts[0]
|
16
16
|
assert_equal 1, posts.size
|
17
|
-
|
17
|
+
|
18
18
|
posts = Post.where { body.like? "%caelum%" }
|
19
19
|
assert_equal @caelum, posts[0]
|
20
20
|
assert_equal 1, posts.size
|
@@ -38,7 +38,7 @@ class DSLTest < ActiveSupport::TestCase
|
|
38
38
|
posts = Post.where(:comments).exists?
|
39
39
|
assert_equal @caelum, posts[0]
|
40
40
|
assert_equal 1, posts.size
|
41
|
-
|
41
|
+
|
42
42
|
posts = Post.where { comments.exists? }
|
43
43
|
assert_equal @caelum, posts[0]
|
44
44
|
assert_equal 1, posts.size
|
@@ -61,7 +61,7 @@ class DSLTest < ActiveSupport::TestCase
|
|
61
61
|
test "exists posts with more than or equals 2 comments" do
|
62
62
|
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
63
63
|
@guilherme.update_attributes(:comments => [Comment.create, Comment.create, Comment.create])
|
64
|
-
|
64
|
+
|
65
65
|
posts = Post.where(:comments).count.ge(2).all
|
66
66
|
assert_equal 2, posts.size
|
67
67
|
assert_equal @caelum, posts[0]
|
@@ -71,22 +71,11 @@ class DSLTest < ActiveSupport::TestCase
|
|
71
71
|
test "dsl query supports first" do
|
72
72
|
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
73
73
|
@guilherme.update_attributes(:comments => [Comment.create, Comment.create, Comment.create])
|
74
|
-
|
74
|
+
|
75
75
|
posts = Post.where(:comments).count.ge(2).first
|
76
76
|
assert_equal @caelum, posts
|
77
77
|
end
|
78
|
-
|
79
|
-
# pending
|
80
|
-
# test "all post which commits has some description" do
|
81
|
-
# comment = Comment.create :description => "dsl test"
|
82
|
-
# @caelum.update_attributes :comments => [comment]
|
83
|
-
#
|
84
|
-
# posts = Post.where(:comments).description.like?("%dsl test%")
|
85
|
-
# assert_equal @caelum, posts[0]
|
86
|
-
# assert_equal 1, posts.size
|
87
|
-
#
|
88
|
-
# end
|
89
|
-
|
78
|
+
|
90
79
|
test "exists posts using strict extended methods" do
|
91
80
|
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
92
81
|
@guilherme.update_attributes(:comments => [Comment.create, Comment.create, Comment.create])
|
@@ -95,14 +84,14 @@ class DSLTest < ActiveSupport::TestCase
|
|
95
84
|
assert_equal 2, posts.size
|
96
85
|
assert_equal @guilherme, posts[1]
|
97
86
|
end
|
98
|
-
|
87
|
+
|
99
88
|
test "strict block supports first" do
|
100
89
|
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
101
90
|
@guilherme.update_attributes(:comments => [Comment.create, Comment.create, Comment.create])
|
102
91
|
post = Post.where { comments >= 2 }.first
|
103
92
|
assert_equal @caelum, post
|
104
93
|
end
|
105
|
-
|
94
|
+
|
106
95
|
test "exists posts using range expectations" do
|
107
96
|
@caelum.update_attributes :published_at => 1.year.ago
|
108
97
|
|
@@ -110,15 +99,6 @@ class DSLTest < ActiveSupport::TestCase
|
|
110
99
|
assert_equal @caelum, posts[0]
|
111
100
|
assert_equal 1, posts.size
|
112
101
|
end
|
113
|
-
|
114
|
-
# pending
|
115
|
-
# test "all post which commits has some subject" do
|
116
|
-
# comment = Comment.create :subject => "dsl subject"
|
117
|
-
# @caelum.update_attributes :comments => [comment]
|
118
|
-
# posts = Post.where(:comments).subject.like?("%dsl subject%")
|
119
|
-
# assert_equal @caelum, posts[0]
|
120
|
-
# assert_equal 1, posts.size
|
121
|
-
# end
|
122
102
|
|
123
103
|
test "accepts two conditions inline" do
|
124
104
|
@caelum.update_attributes :published_at => 1.year.ago
|
@@ -131,7 +111,7 @@ class DSLTest < ActiveSupport::TestCase
|
|
131
111
|
assert_equal @caelum, posts[0]
|
132
112
|
assert_equal 1, posts.size
|
133
113
|
end
|
134
|
-
|
114
|
+
|
135
115
|
test "supports two conditions in dsl mixing everything together" do
|
136
116
|
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
137
117
|
@guilherme.update_attributes(:comments => [Comment.create, Comment.create, Comment.create])
|
@@ -139,21 +119,27 @@ class DSLTest < ActiveSupport::TestCase
|
|
139
119
|
assert_equal @caelum, posts[0]
|
140
120
|
assert_equal 1, posts.size
|
141
121
|
end
|
142
|
-
|
122
|
+
|
143
123
|
test "supports == with relation count" do
|
144
124
|
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
145
125
|
posts = Post.where { comments == 2 }
|
146
126
|
assert_equal @caelum, posts[0]
|
147
127
|
assert_equal 1, posts.size
|
148
128
|
end
|
149
|
-
|
129
|
+
|
150
130
|
test "supports == with simple field" do
|
151
|
-
@caelum.update_attributes(:comments => [Comment.create, Comment.create])
|
152
131
|
posts = Post.where { body == "CaelumObjects training and inovation" }
|
153
132
|
assert_equal @caelum, posts[0]
|
154
133
|
assert_equal 1, posts.size
|
155
134
|
end
|
156
135
|
|
136
|
+
# test "supports != with simple field" do
|
137
|
+
# posts = Post.where { body != "diff" }
|
138
|
+
# assert_equal @caelum, posts[0]
|
139
|
+
# assert_equal @guilherme, posts[1]
|
140
|
+
# assert_equal 2, posts.size
|
141
|
+
# end
|
142
|
+
|
157
143
|
test "accepts two conditions one after the other" do
|
158
144
|
@caelum.update_attributes :published_at => 1.year.ago
|
159
145
|
@guilherme.update_attributes :published_at => 1.year.ago
|
@@ -172,6 +158,14 @@ class DSLTest < ActiveSupport::TestCase
|
|
172
158
|
assert_equal 1, posts.size
|
173
159
|
end
|
174
160
|
|
161
|
+
test "give records with nil fields" do
|
162
|
+
@caelum.update_attributes :body => nil
|
163
|
+
posts = Post.where(:body).is_null
|
164
|
+
assert_equal @caelum, posts[0]
|
165
|
+
assert_equal 1, posts.size
|
166
|
+
end
|
167
|
+
|
168
|
+
|
175
169
|
# test "second level relation in a dsl" do
|
176
170
|
# comment = Comment.create :description => "dsl test"
|
177
171
|
# @caelum.update_attributes :comments => [comment]
|
@@ -1,55 +1,55 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__)) + "/test_helper"
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'relata/filter'
|
4
4
|
require 'schema'
|
5
5
|
|
6
6
|
class FilteredRelationTest < ActiveSupport::TestCase
|
7
7
|
setup do
|
8
8
|
setup_db
|
9
9
|
create_posts
|
10
|
+
end
|
11
|
+
|
12
|
+
test "given no values to filtered_relation, gives us all records" do
|
13
|
+
assert_equal Post.all, Post.filtered_relation({}).all
|
14
|
+
end
|
15
|
+
|
16
|
+
test "given a content and comment filter, gives us filtered records - generic" do
|
17
|
+
@base.update_attributes(:content => "picture", :comments => [Comment.create])
|
18
|
+
assert_equal @base, Post.filtered_relation(:content => "picture", :comments => 'true').first
|
10
19
|
end
|
11
20
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
test "given a content and comment filter, gives us filtered records" do
|
22
|
+
@base.update_attributes(:content => "picture", :comments => [Comment.create])
|
23
|
+
assert_equal @base, Post.filtered_relation(:content => "picture").related_to(:comments => true).first
|
24
|
+
end
|
25
|
+
|
26
|
+
test "given a date and comment filter, gives us filtered records" do
|
27
|
+
@base.update_attributes(:published_at => 2.years.ago, :comments => [Comment.create])
|
28
|
+
assert_equal @base, Post.filtered_relation(:published_at => true).related_to(:comments => true).first
|
29
|
+
end
|
30
|
+
|
31
|
+
test "given a date and content filter, gives us filtered records" do
|
32
|
+
@base.update_attribute(:published_at, 2.years.ago)
|
33
|
+
@base.update_attribute(:content, "picture")
|
34
|
+
record = Post.filtered_relation(:published_at => true, :content => "picture").first
|
25
35
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
36
|
+
assert_equal @base, record
|
37
|
+
end
|
38
|
+
|
39
|
+
test "given two dates, gives us filtered records between this date" do
|
40
|
+
assert_equal @base, Post.date_between(:before => 1.year.ago, :after => Time.now).first
|
41
|
+
end
|
42
|
+
|
43
|
+
test "return a post with same title" do
|
44
|
+
@base.update_attributes(:title => "Post Title")
|
45
|
+
assert_equal @base, Post.filtered_relation(:title => "Post Title").first
|
46
|
+
end
|
47
|
+
|
48
|
+
test "return a post with same title and body" do
|
49
|
+
@base.update_attributes(:title => "Post Title", :body => "Ruby")
|
50
|
+
assert_equal @base, Post.filtered_relation(:title => "Post Title", :body => "Ruby").first
|
51
|
+
end
|
35
52
|
|
36
|
-
assert_equal @base, record
|
37
|
-
end
|
38
|
-
|
39
|
-
test "given two dates, gives us filtered records between this date" do
|
40
|
-
assert_equal @base, Post.date_between(:before => 1.year.ago, :after => Time.now).first
|
41
|
-
end
|
42
|
-
|
43
|
-
test "return a post with same title" do
|
44
|
-
@base.update_attributes(:title => "Post Title")
|
45
|
-
assert_equal @base, Post.filtered_relation(:title => "Post Title").first
|
46
|
-
end
|
47
|
-
|
48
|
-
test "return a post with same title and body" do
|
49
|
-
@base.update_attributes(:title => "Post Title", :body => "Ruby")
|
50
|
-
assert_equal @base, Post.filtered_relation(:title => "Post Title", :body => "Ruby").first
|
51
|
-
end
|
52
|
-
|
53
53
|
|
54
54
|
def create_posts
|
55
55
|
valid_attributes = {
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + "/test_helper"
|
2
|
+
require 'schema'
|
3
|
+
require 'relata/dsl'
|
4
|
+
|
5
|
+
class DSLTest < ActiveSupport::TestCase
|
6
|
+
|
7
|
+
setup do
|
8
|
+
setup_db
|
9
|
+
@caelum = Post.create :body => "CaelumObjects training and inovation"
|
10
|
+
@guilherme = Post.create :body => "Guilherme Silveira"
|
11
|
+
end
|
12
|
+
|
13
|
+
test "all post which commits has some subject" do
|
14
|
+
comment = Comment.create :subject => "dsl subject"
|
15
|
+
@caelum.update_attributes :comments => [comment]
|
16
|
+
posts = Post.where(:comments).subject.like?("%dsl subject%")
|
17
|
+
assert_equal @caelum, posts[0]
|
18
|
+
assert_equal 1, posts.size
|
19
|
+
end
|
20
|
+
|
21
|
+
test "all post which commits has some description" do
|
22
|
+
comment = Comment.create :description => "dsl test"
|
23
|
+
@caelum.update_attributes :comments => [comment]
|
24
|
+
|
25
|
+
posts = Post.where(:comments).description.like?("%dsl test%")
|
26
|
+
assert_equal @caelum, posts[0]
|
27
|
+
assert_equal 1, posts.size
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
test "all post which commits has some subject and another has_many relation" do
|
32
|
+
reader = Reader.create :name => "anderson"
|
33
|
+
comment = Comment.create :subject => "dsl subject"
|
34
|
+
@caelum.update_attributes :comments => [comment], :readers => [reader]
|
35
|
+
posts = Post.where(:comments).subject.like?("%dsl subject%")
|
36
|
+
assert_equal @caelum, posts[0]
|
37
|
+
assert_equal 1, posts.size
|
38
|
+
end
|
39
|
+
|
40
|
+
test "exists posts for a specific user name" do
|
41
|
+
user = User.create :name => "anderson"
|
42
|
+
@caelum.update_attributes(:user => user)
|
43
|
+
posts = Post.where(:user).name.like?("anderson")
|
44
|
+
assert_equal @caelum, posts[0]
|
45
|
+
assert_equal 1, posts.size
|
46
|
+
end
|
47
|
+
|
48
|
+
test "exists posts for a specific user title" do
|
49
|
+
user = User.create :title => "programmer"
|
50
|
+
@caelum.update_attributes(:user => user)
|
51
|
+
posts = Post.where(:user).title.like?("programmer")
|
52
|
+
assert_equal @caelum, posts[0]
|
53
|
+
assert_equal 1, posts.size
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
end
|
data/test/schema.rb
CHANGED
@@ -20,6 +20,8 @@ def setup_db
|
|
20
20
|
end
|
21
21
|
|
22
22
|
create_table :users do |t|
|
23
|
+
t.string :name
|
24
|
+
t.text :title
|
23
25
|
t.timestamps
|
24
26
|
end
|
25
27
|
|
@@ -29,6 +31,13 @@ def setup_db
|
|
29
31
|
t.integer :post_id
|
30
32
|
t.timestamps
|
31
33
|
end
|
34
|
+
|
35
|
+
create_table :readers do |t|
|
36
|
+
t.text :name
|
37
|
+
t.integer :post_id
|
38
|
+
t.timestamps
|
39
|
+
end
|
40
|
+
|
32
41
|
|
33
42
|
end
|
34
43
|
end
|
@@ -36,6 +45,7 @@ end
|
|
36
45
|
class Post < ActiveRecord::Base
|
37
46
|
belongs_to :user
|
38
47
|
has_many :comments
|
48
|
+
has_many :readers
|
39
49
|
end
|
40
50
|
|
41
51
|
class User < ActiveRecord::Base
|
@@ -46,3 +56,7 @@ class Comment < ActiveRecord::Base
|
|
46
56
|
belongs_to :post
|
47
57
|
end
|
48
58
|
|
59
|
+
class Reader < ActiveRecord::Base
|
60
|
+
belongs_to :post
|
61
|
+
end
|
62
|
+
|
metadata
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
- Anderson Leite, Guilherme Silveira
|
13
|
+
- Anderson Leite, Guilherme Silveira, Pedro Mariano
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-29 00:00:00 -02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -36,22 +36,23 @@ files:
|
|
36
36
|
- README.markdown
|
37
37
|
- test.sqlite3
|
38
38
|
- uninstall.rb
|
39
|
-
- lib/filtered_relation.rb
|
40
39
|
- lib/relata/dsl/conditions.rb
|
41
40
|
- lib/relata/dsl/constraints.rb
|
42
41
|
- lib/relata/dsl/custom_relation.rb
|
43
42
|
- lib/relata/dsl/field_search.rb
|
44
43
|
- lib/relata/dsl/missed_builder.rb
|
44
|
+
- lib/relata/dsl/querys/belongs.rb
|
45
45
|
- lib/relata/dsl/querys/fields.rb
|
46
46
|
- lib/relata/dsl/querys/multiple.rb
|
47
47
|
- lib/relata/dsl/querys/simple.rb
|
48
48
|
- lib/relata/dsl.rb
|
49
49
|
- lib/relata/filter.rb
|
50
50
|
- lib/relata/related_query_methods.rb
|
51
|
+
- lib/relata.rb
|
51
52
|
- test/dsl_test.rb
|
52
53
|
- test/filtered_relation_test.rb
|
54
|
+
- test/relation_dsl_test.rb
|
53
55
|
- test/schema.rb
|
54
|
-
- test/test.sqlite3
|
55
56
|
- test/test_helper.rb
|
56
57
|
has_rdoc: false
|
57
58
|
homepage: http://github.com/caelum/relata
|
data/lib/filtered_relation.rb
DELETED
data/test/test.sqlite3
DELETED
Binary file
|