paraphrase 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/src/paraphrase
3
3
  specs:
4
- paraphrase (0.6.0)
4
+ paraphrase (0.6.1)
5
5
  activerecord (~> 3.0)
6
6
  activesupport (~> 3.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/src/paraphrase
3
3
  specs:
4
- paraphrase (0.6.0)
4
+ paraphrase (0.6.1)
5
5
  activerecord (~> 3.0)
6
6
  activesupport (~> 3.0)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/src/paraphrase
3
3
  specs:
4
- paraphrase (0.6.0)
4
+ paraphrase (0.6.1)
5
5
  activerecord (~> 3.0)
6
6
  activesupport (~> 3.0)
7
7
 
@@ -64,7 +64,7 @@ module Paraphrase
64
64
  return relation
65
65
  end
66
66
 
67
- scope.arity == 0 ? scope.call : scope.call(*inputs)
67
+ scope.arity == 0 ? relation.send(method_name) : relation.send(method_name, *inputs)
68
68
  end
69
69
 
70
70
  private
@@ -1,3 +1,3 @@
1
1
  module Paraphrase
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -2,10 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  module Paraphrase
4
4
  describe Query do
5
- subject { AccountQuery }
6
- let(:query) { Account.paraphrase(:name => 'Tyrion Lannister', :nickname => 'Half Man') }
7
-
8
5
  describe ".map" do
6
+ subject { Class.new(Query) }
7
+
9
8
  it "adds information to Query.mappings" do
10
9
  subject.map :name_like, :to => :name
11
10
 
@@ -13,11 +12,21 @@ module Paraphrase
13
12
  end
14
13
 
15
14
  it "raises an error if a scope is added twice" do
15
+ subject.map :name_like, :to => :name
16
+
16
17
  expect { subject.map :name_like, :to => :name }.to raise_error Paraphrase::DuplicateScopeError
17
18
  end
18
19
  end
19
20
 
20
21
  describe "on initialization" do
22
+ let(:query) do
23
+ klass = Class.new(Query) do
24
+ map :name_like, :to => :name
25
+ end
26
+
27
+ klass.new({ :name => '', :nickname => '' }, Account)
28
+ end
29
+
21
30
  it "filters out params not specified in mappings" do
22
31
  query.params.should_not have_key :nickname
23
32
  query.params.should have_key :name
@@ -29,22 +38,47 @@ module Paraphrase
29
38
  end
30
39
 
31
40
  describe "#results" do
32
- before :all do
33
- subject.map :title_like, :to => :title, :require => true
41
+ let(:klass) do
42
+ Class.new(Query) do
43
+ map :name_like, :to => :name
44
+ map :title_like, :to => :title, :require => true
45
+ end
34
46
  end
35
47
 
36
48
  it "loops through scope methods and applies them to source" do
37
49
  Account.should_receive(:title_like).and_return(Account.scoped)
38
50
  Account.should_receive(:name_like).and_return(Account.scoped)
39
51
 
40
- query = Account.paraphrase(:name => 'Jon Snow', :title => 'Wall Watcher')
52
+ query = klass.new({ :name => 'Jon Snow', :title => 'Wall Watcher'}, Account)
41
53
  query.results
42
54
  end
43
55
 
44
56
  it "returns empty array if inputs were missing and required" do
45
- query = Account.paraphrase({})
57
+ query = klass.new({}, Account)
46
58
  query.results.should eq []
47
59
  end
48
60
  end
61
+
62
+ describe "preserves" do
63
+ let(:accounts) do
64
+ user = User.create!
65
+ user.accounts << Account.create!
66
+ end
67
+
68
+ let(:results) do
69
+ klass = Class.new(Query) do
70
+ map :name_like, :to => :name
71
+ end
72
+
73
+ klass.new({ :name => 'name' }, accounts).results.to_a
74
+ end
75
+
76
+ it "the relation passed in during initialization" do
77
+ # Create an extra account that shouldn't be in the results
78
+ Account.create!
79
+
80
+ results.should eq accounts.to_a
81
+ end
82
+ end
49
83
  end
50
84
  end
@@ -2,36 +2,45 @@ require 'spec_helper'
2
2
 
3
3
  module Paraphrase
4
4
  describe ScopeMapping do
5
- let(:mapping) { ScopeMapping.new(:name_like, :to => :name) }
6
- let(:compound_mapping) { ScopeMapping.new(:name_like, :to => [:first_name, :last_name], :require => :last_name) }
7
- let(:query) { Account.scoped }
8
5
 
9
6
  describe "#chain" do
10
- it "applies scope method to query object with values from params hash" do
7
+ let(:mapping) { ScopeMapping.new(:name_like, :to => :name) }
8
+
9
+ it "applies scope method to Account.scoped object with values from params hash" do
11
10
  Account.should_receive(:name_like).with('Jon Snow')
12
- mapping.chain({ :name => 'Jon Snow' }, query)
11
+
12
+ mapping.chain({ :name => 'Jon Snow' }, Account.scoped)
13
13
  end
14
14
 
15
15
  it "does nothing if values are missing" do
16
16
  Account.should_not_receive(:name_like).with('Jon Snow')
17
- mapping.chain({}, query)
17
+
18
+ mapping.chain({}, Account.scoped)
18
19
  end
19
20
 
20
21
  it "passes through nil values if scope has been whitelisted" do
21
22
  mapping = ScopeMapping.new(:name_like, :to => :name, :whitelist => true)
22
23
 
23
24
  Account.should_receive(:name_like).with(nil)
24
- mapping.chain({}, query)
25
+
26
+ mapping.chain({}, Account.scoped)
25
27
  end
26
28
  end
27
29
 
28
- it "can require a subset of a compound key" do
29
- Account.should_receive(:name_like).with(nil, 'Lannister')
30
- compound_mapping.chain({ :last_name => 'Lannister' }, query)
31
- end
30
+ describe "compound keys" do
31
+ let(:compound_mapping) do
32
+ ScopeMapping.new(:name_like, :to => [:first_name, :last_name], :require => :last_name)
33
+ end
34
+
35
+ it "can require a subset of a compound key" do
36
+ Account.should_receive(:name_like).with(nil, 'Lannister')
32
37
 
33
- it "whitelists the the non-required keys of a compound key" do
34
- compound_mapping.whitelist.include?(:first_name).should be_true
38
+ compound_mapping.chain({ :last_name => 'Lannister' }, Account.scoped)
39
+ end
40
+
41
+ it "whitelists the the non-required keys of a compound key" do
42
+ compound_mapping.whitelist.include?(:first_name).should be_true
43
+ end
35
44
  end
36
45
  end
37
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paraphrase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-16 00:00:00.000000000 Z
12
+ date: 2012-09-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord