consul 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of consul might be problematic. Click here for more details.

data/lib/consul/power.rb CHANGED
@@ -35,19 +35,44 @@ module Consul
35
35
  include?(*args) or raise Consul::Powerless.new("No power to #{args.inspect}")
36
36
  end
37
37
 
38
+ def name_for_record(*args)
39
+ adjective, record = Util.adjective_and_argument(*args)
40
+ name_for_model(adjective, record.class)
41
+ end
42
+
38
43
  def for_record(*args)
44
+ send(name_for_record(*args))
45
+ end
46
+
47
+ def include_record?(*args)
39
48
  adjective, record = Util.adjective_and_argument(*args)
40
- for_model(adjective, record.class)
49
+ include?(name_for_record(*args), record)
41
50
  end
42
51
 
43
- def for_model(*args)
52
+ def include_record!(*args)
53
+ adjective, record = Util.adjective_and_argument(*args)
54
+ include!(name_for_record(*args), record)
55
+ end
56
+
57
+ def name_for_model(*args)
44
58
  adjective, model_class = Util.adjective_and_argument(*args)
45
59
  collection_name = model_class.name.underscore.gsub('/', '_').pluralize
46
60
  [adjective, collection_name].select(&:present?).join('_')
47
61
  end
48
62
 
49
- private
63
+ def for_model(*args)
64
+ send(name_for_model(*args))
65
+ end
50
66
 
67
+ def include_model?(*args)
68
+ include?(name_for_model(*args))
69
+ end
70
+
71
+ def include_model!(*args)
72
+ include!(name_for_model(*args))
73
+ end
74
+
75
+ private
51
76
 
52
77
  def boolean_or_nil?(value)
53
78
  [TrueClass, FalseClass, NilClass].include?(value.class)
@@ -87,6 +112,22 @@ module Consul
87
112
  end
88
113
  end
89
114
 
115
+ def include_model?(*args)
116
+ if current
117
+ current.include_model?(*args)
118
+ else
119
+ true
120
+ end
121
+ end
122
+
123
+ def include_model!(*args)
124
+ if current
125
+ current.include_model!(*args)
126
+ else
127
+ true
128
+ end
129
+ end
130
+
90
131
  def for_record(*args)
91
132
  if current
92
133
  current.for_record(*args)
@@ -96,6 +137,22 @@ module Consul
96
137
  end
97
138
  end
98
139
 
140
+ def include_record?(*args)
141
+ if current
142
+ current.include_record?(*args)
143
+ else
144
+ true
145
+ end
146
+ end
147
+
148
+ def include_record!(*args)
149
+ if current
150
+ current.include_record!(*args)
151
+ else
152
+ true
153
+ end
154
+ end
155
+
99
156
  private
100
157
 
101
158
  def define_power(name, &block)
@@ -1,3 +1,3 @@
1
1
  module Consul
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
@@ -74,13 +74,16 @@ class Power
74
74
  end
75
75
 
76
76
  power :deals do
77
- 'deals'
77
+ 'deals power' unless guest?
78
78
  end
79
79
 
80
80
  power :updatable_deals do
81
- 'updatable_deals'
81
+ 'updatable_deals power' unless guest?
82
82
  end
83
83
 
84
+ power :deal_items do
85
+ 'deal_items power'
86
+ end
84
87
 
85
88
  private
86
89
 
@@ -434,15 +434,15 @@ describe Consul::Power do
434
434
  describe '#for_model' do
435
435
 
436
436
  it 'should return the power corresponding to the given model' do
437
- @user.power.for_model(Deal).should == 'deals'
437
+ @user.power.for_model(Deal).should == 'deals power'
438
438
  end
439
439
 
440
440
  it 'should return the correct power for a namespaced model' do
441
- @user.power.for_model(Deal::Item).should == 'deal_items'
441
+ @user.power.for_model(Deal::Item).should == 'deal_items power'
442
442
  end
443
443
 
444
444
  it 'should allow to prefix the power with an adjective' do
445
- @user.power.for_model(:updatable, Deal).should == 'updatable_deals'
445
+ @user.power.for_model(:updatable, Deal).should == 'updatable_deals power'
446
446
  end
447
447
 
448
448
  end
@@ -452,14 +452,14 @@ describe Consul::Power do
452
452
  context 'when Power.current is present' do
453
453
 
454
454
  it 'should return the power corresponding to the given model' do
455
- Power.with_power(Power.new) do
456
- Power.for_model(Deal).should == 'deals'
455
+ Power.with_power(@user.power) do
456
+ Power.for_model(Deal).should == 'deals power'
457
457
  end
458
458
  end
459
459
 
460
460
  it 'should allow to prefix the power with an adjective' do
461
- Power.with_power(Power.new) do
462
- Power.for_model(:updatable, Deal).should == 'updatable_deals'
461
+ Power.with_power(@user.power) do
462
+ Power.for_model(:updatable, Deal).should == 'updatable_deals power'
463
463
  end
464
464
  end
465
465
 
@@ -479,10 +479,45 @@ describe Consul::Power do
479
479
 
480
480
  end
481
481
 
482
+ describe '#include_model?' do
483
+
484
+ it 'should return if the given model corresponds to a non-nil power' do
485
+ @user.role = 'guest'
486
+ @user.power.include_model?(Client).should be_false
487
+ @user.role = 'admin'
488
+ @user.power.include_model?(Client).should be_true
489
+ end
490
+
491
+ end
492
+
493
+ describe '.include_model?' do
494
+
495
+ context 'when Power.current is present' do
496
+
497
+ it 'should return whether the given model corresponds to a non-nil power' do
498
+ Power.with_power(@user.power) do
499
+ @user.role = 'guest'
500
+ Power.include_model?(Deal).should be_false
501
+ @user.role = 'admin'
502
+ Power.include_model?(Deal).should be_true
503
+ end
504
+ end
505
+ end
506
+
507
+ context 'when Power.current is nil' do
508
+
509
+ it 'should return true' do
510
+ Power.include_model?(Deal).should be_true
511
+ end
512
+
513
+ end
514
+
515
+ end
516
+
482
517
  describe '#for_record' do
483
518
 
484
519
  it 'should return the power corresponding to the class of the given record' do
485
- @user.power.for_record(Deal.new).should == 'deals'
520
+ @user.power.for_record(Deal.new).should == 'deals power'
486
521
  end
487
522
 
488
523
  end
@@ -492,14 +527,14 @@ describe Consul::Power do
492
527
  context 'when Power.current is present' do
493
528
 
494
529
  it 'should return the power corresponding to the class of the given record' do
495
- Power.with_power(Power.new) do
496
- Power.for_record(Deal.new).should == 'deals'
530
+ Power.with_power(@user.power) do
531
+ Power.for_record(Deal.new).should == 'deals power'
497
532
  end
498
533
  end
499
534
 
500
535
  it 'should allow to prefix the power with an adjective' do
501
- Power.with_power(Power.new) do
502
- Power.for_record(:updatable, Deal.new).should == 'updatable_deals'
536
+ Power.with_power(@user.power) do
537
+ Power.for_record(:updatable, Deal.new).should == 'updatable_deals power'
503
538
  end
504
539
  end
505
540
 
@@ -507,11 +542,11 @@ describe Consul::Power do
507
542
 
508
543
  context 'when Power.current is nil' do
509
544
 
510
- it 'should return the given model' do
545
+ it 'should return true' do
511
546
  Power.for_record(Deal.new).should == Deal
512
547
  end
513
548
 
514
- it 'should return the given model even if the model was prefixed with an adjective' do
549
+ it 'should return true even if the model was prefixed with an adjective' do
515
550
  Power.for_record(:updatable, Deal.new).should == Deal
516
551
  end
517
552
 
@@ -519,4 +554,36 @@ describe Consul::Power do
519
554
 
520
555
  end
521
556
 
557
+ describe '#include_record?' do
558
+
559
+ it 'should return if the given record is included in the power corresponding to the class of the given record' do
560
+ @user.power.include_record?(@deleted_client).should be_false
561
+ @user.power.include_record?(@client1).should be_true
562
+ end
563
+
564
+ end
565
+
566
+ describe '.include_record?' do
567
+
568
+ context 'when Power.current is present' do
569
+
570
+ it 'should return whether the given record is included in the the power corresponding to the class of the given record' do
571
+ Power.with_power(@user.power) do
572
+ Power.include_record?(@deleted_client).should be_false
573
+ Power.include_record?(@client1).should be_true
574
+ end
575
+ end
576
+ end
577
+
578
+ context 'when Power.current is nil' do
579
+
580
+ it 'should return true' do
581
+ Power.include_record?(Deal.new).should be_true
582
+ end
583
+
584
+ end
585
+
586
+ end
587
+
588
+
522
589
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 0
10
- version: 0.6.0
9
+ - 1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-15 00:00:00 +01:00
18
+ date: 2013-01-16 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency