guise 0.3.0 → 0.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7c5c286fa539febf7ea6219aab2a407081b7b12
4
- data.tar.gz: 848f2e32a8bcbde9fefc49fb30fc1728e8e7c989
3
+ metadata.gz: 823b5e9bab357ec221cde51785d6c6e8707e7ae2
4
+ data.tar.gz: 033b80fb3da59c5778ab1f8f566bfa0cf8c37976
5
5
  SHA512:
6
- metadata.gz: d114f4f75bcdfc5dad56b148292883812bdbd13c0153179cbee1347f62864fab1391f68b9c17f4bd59eeacd59ac299c4eb7e3498cc6f975d75e37b3c5cd12d9b
7
- data.tar.gz: d7060c18f0b08727ec9a03d1bb96f6e835ea30b5ae915fa95012af4179832aa2e289de1d66a4e493510a2fd166dcc8fd11c4bf2e45d55c236fc82d69ae0d1d78
6
+ metadata.gz: 1f737e5cfa5dfbd9b7922299a4ec383af710571179d4de71503d92342558f9ac331fe83bb94b129ac1e8f036f37a09ddadd6df41ad13f3b1d215a3a30fe8bd9b
7
+ data.tar.gz: 961af27e3a10047502402cb1c6c556ab7c0be21365c28aabf6e23b065415f3978640639991efe985800e83502feb46bbde254bfce520896b4d5401fbea70170f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- guise (0.3.0)
4
+ guise (0.3.1)
5
5
  activerecord (>= 3.1, < 4.1)
6
6
  activesupport (>= 3.1, < 4.1)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- guise (0.3.0)
4
+ guise (0.3.1)
5
5
  activerecord (>= 3.1, < 4.1)
6
6
  activesupport (>= 3.1, < 4.1)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- guise (0.3.0)
4
+ guise (0.3.1)
5
5
  activerecord (>= 3.1, < 4.1)
6
6
  activesupport (>= 3.1, < 4.1)
7
7
 
data/lib/guise/syntax.rb CHANGED
@@ -13,6 +13,7 @@ module Guise
13
13
  guises = guises.map(&:to_s)
14
14
  association = options.fetch(:association)
15
15
  attribute = options.fetch(:attribute)
16
+ table_name = options[:table_name] || association
16
17
 
17
18
  Guise.registry[self.name] = {
18
19
  names: guises,
@@ -22,14 +23,14 @@ module Guise
22
23
 
23
24
  guises.each do |guise|
24
25
  method_name = guise.underscore
25
- scope method_name.pluralize, -> { joins(association).where(association => { attribute => guise }) }
26
+ scope method_name.pluralize, -> { joins(association).where(table_name => { attribute => guise }) }
26
27
 
27
28
  define_method "#{method_name}?" do
28
29
  has_guise?(guise)
29
30
  end
30
31
  end
31
32
 
32
- has_many association, options.except(:association, :attribute)
33
+ has_many association, options.except(:association, :attribute, :table_name)
33
34
 
34
35
  if association != :guises
35
36
  association_singular = association.to_s.singularize
@@ -66,7 +67,7 @@ module Guise
66
67
  association = class_name.to_s.underscore.to_sym
67
68
  guises = guise_options[:names]
68
69
  attribute = guise_options[:attribute]
69
- foreign_key = options[:foreign_key] || "#{class_name.underscore}_id"
70
+ foreign_key = options[:foreign_key] || "#{class_name.to_s.underscore}_id"
70
71
 
71
72
  belongs_to association, options.except(:validate)
72
73
 
data/lib/guise/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Guise
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/spec/factories.rb CHANGED
@@ -17,6 +17,14 @@ FactoryGirl.define do
17
17
  end
18
18
  end
19
19
 
20
+ factory :person do
21
+ end
22
+
23
+ factory :permission do
24
+ person
25
+ privilege 'Admin'
26
+ end
27
+
20
28
  factory :user_role do
21
29
  user
22
30
  name 'Technician'
data/spec/guise_spec.rb CHANGED
@@ -22,6 +22,14 @@ describe Guise do
22
22
  expect(technicians).not_to include user
23
23
  expect(technicians).not_to include supervisor
24
24
  end
25
+
26
+ it 'handles non-standard table names and foreign key attributes' do
27
+ person = create(:person)
28
+ create(:permission, person: person)
29
+
30
+ expect(person).to have_many :permissions
31
+ expect(Person.admins).to include person
32
+ end
25
33
  end
26
34
 
27
35
  describe "#has_guise?" do
@@ -126,7 +134,7 @@ describe Guise do
126
134
  end
127
135
 
128
136
  it "unique per resource" do
129
- should validate_uniqueness_of(:name).scoped_to(:person_id)
137
+ should validate_uniqueness_of(:name).scoped_to(:user_id)
130
138
  end
131
139
 
132
140
  it "is one of the guise names provided" do
data/spec/spec_helper.rb CHANGED
@@ -21,12 +21,20 @@ ActiveRecord::Schema.define do
21
21
 
22
22
  create_table :user_roles, force: true do |t|
23
23
  t.string :name
24
- t.integer :person_id
24
+ t.integer :user_id
25
+ end
26
+
27
+ create_table :people, force: true do |t|
28
+ end
29
+
30
+ create_table :privileges, force: true do |t|
31
+ t.integer :employee_id
32
+ t.string :privilege
25
33
  end
26
34
  end
27
35
 
28
36
  class User < ActiveRecord::Base
29
- has_guises :Technician, :Supervisor, association: :user_roles, attribute: :name, foreign_key: :person_id
37
+ has_guises :Technician, :Supervisor, association: :user_roles, attribute: :name
30
38
  end
31
39
 
32
40
  class Technician < User
@@ -38,14 +46,22 @@ class Supervisor < User
38
46
  end
39
47
 
40
48
  class UserRole < ActiveRecord::Base
41
- guise_for :User,
42
- foreign_key: :person_id
49
+ guise_for :User
43
50
  end
44
51
 
45
52
  class TechnicianUserRole < UserRole
46
53
  scoped_guise_for :User
47
54
  end
48
55
 
56
+ class Person < ActiveRecord::Base
57
+ has_guises :Admin, :Manager, :Reviewer, association: :permissions, attribute: :privilege, foreign_key: :employee_id, table_name: :privileges
58
+ end
59
+
60
+ class Permission < ActiveRecord::Base
61
+ self.table_name = :privileges
62
+
63
+ guise_for :Person, foreign_key: :employee_id
64
+ end
49
65
 
50
66
  FactoryGirl.find_definitions
51
67
  RSpec.configure do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Gutierrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-04 00:00:00.000000000 Z
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord