rollable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # TODO: Because of the endless blocks, some calls are too nested, needs to be fixed. Even more seperate methods
2
+ # in seperate files would clean in up.
1
3
  module Rollable
2
4
  module Base
3
5
  def self.included(base)
@@ -19,12 +21,34 @@ module Rollable
19
21
  # Hackety hack.
20
22
  class << self; attr_reader :rollables, :role_names; end;
21
23
  end
22
-
24
+
25
+ set_target_model_helpers
23
26
  set_relations_on_rollables
24
27
  set_role_validations
25
28
  end
26
29
 
27
30
  private
31
+
32
+ # DISCLAIMER: Headache and on the airport, hush.
33
+ # Helpers for target models like has_owner? and get_owners
34
+ def set_target_model_helpers
35
+ @rollables.select { |r| r.is_a?(String) }.each do |model|
36
+ model = model.constantize
37
+ @role_names.each do |role_name|
38
+ model.class_eval do
39
+ define_method("has_#{role_name}?") do
40
+ self.roles.where("name = ? ", role_name).count > 0
41
+ end
42
+
43
+ # TODO: get_#{role_name} doesn't sound very rubyish
44
+ define_method("get_#{role_name.pluralize}") do
45
+ self.roles.where("name = ? ", role_name).collect(&:user)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
28
52
  def set_role_validations
29
53
  # Yugh, scope and self, ugly but it works.
30
54
  rollables = @rollables
@@ -47,12 +71,14 @@ module Rollable
47
71
  has_many :roles, :as => 'rollable' # Define the relationship <img src="http://www.timonv.nl/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley">
48
72
  end
49
73
  else
74
+ # TODO: Check should be made earlier
50
75
  raise "#{model} is not an ActiveRecord object!" #Again, just to be sure and nice.
51
76
  end
52
77
  end
53
78
  end
54
79
  end
55
80
 
81
+ # TODO: Don't need method_missing no more, use dynamic dispatch instead.
56
82
  def method_missing(method, *args)
57
83
  if method =~ /^is_([a-z]+)(?:_(?:on|of))?\?$/ #Common spell to match against regex
58
84
  role = $1
@@ -1,3 +1,3 @@
1
1
  module Rollable
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -19,5 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
  s.add_dependency "rails", ">= 3.0.0"
22
- s.add_dependency "rspec"
22
+ s.add_dependency "rspec", "> 2"
23
+ s.add_dependency "rspec-rails"
23
24
  end
@@ -1,5 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Horse do
4
- pending "add some examples to (or delete) #{__FILE__}"
4
+ context "Target helper methods" do
5
+ it "should have the methods" do
6
+ horse = Horse.create
7
+ horse.should respond_to("has_rider?")
8
+ horse.should respond_to("get_riders")
9
+ end
10
+
11
+ it "should respond correctly without roles" do
12
+ horse = Horse.create
13
+ horse.has_rider?.should eq(false)
14
+ horse.get_riders.should eq([])
15
+ end
16
+
17
+ it "should return correct values if roles are set" do
18
+ horse = Horse.create
19
+ user = User.create
20
+ user.roles.create(:rollable => horse, :name => "rider")
21
+ horse.has_rider?.should eq(true)
22
+ horse.get_riders.should include(user)
23
+ end
24
+ end
5
25
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rollable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Timon Vonk
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-23 00:00:00 Z
13
+ date: 2011-05-05 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -27,13 +27,24 @@ dependencies:
27
27
  name: rspec
28
28
  prerelease: false
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">"
33
+ - !ruby/object:Gem::Version
34
+ version: "2"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec-rails
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
30
41
  none: false
31
42
  requirements:
32
43
  - - ">="
33
44
  - !ruby/object:Gem::Version
34
45
  version: "0"
35
46
  type: :runtime
36
- version_requirements: *id002
47
+ version_requirements: *id003
37
48
  description: This gem adds agnostic roles for authorization to Rails.
38
49
  email:
39
50
  - mail@timonv.nl