permissable 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.2
@@ -41,6 +41,12 @@ module Permissable
41
41
  # response array to return
42
42
  result_response = []
43
43
 
44
+ # Load all permissions fresh so we can kill dupes.
45
+ saved_permissions = Permission.all
46
+ # Store new permissions in an array so we can squeeze into one transaction.
47
+ permissions_to_add = []
48
+ permissions_to_update = []
49
+
44
50
  [resources].flatten.each do |resource|
45
51
 
46
52
  # Kind of unecessary but since some methods allow you to specify a Classname directly, this just
@@ -51,21 +57,37 @@ module Permissable
51
57
  identifier = member_identifier(resource)
52
58
 
53
59
  [methods].flatten.each do |method|
54
- # Permission already exists, continue.
55
- next if can?(method, resource, false)
60
+
61
+ resource_type = (resource.respond_to?(:base_class) ? resource.base_class.to_s : resource.class.base_class.to_s)
56
62
 
57
63
  # Create a new permission for each member (once if its self, multiple times if its associated)
58
- [identifier[:member_id]].flatten.each do |member_id|
59
- perm = Permission.where(:member_id => member_id, :member_type => identifier[:member_type]).for_resource(resource).first || Permission.new(:member_id => member_id, :member_type => identifier[:member_type])
64
+ [identifier[:member_id]].flatten.each do |member_id|
65
+ perm = saved_permissions.detect{ |p| p.member_id == member_id && p.member_type == identifier[:member_type] && p.resource_id == resource.id && p.resource_type == resource_type } || Permission.new(:member_id => member_id, :member_type => identifier[:member_type])
60
66
  perm.permission_type = method.to_s.downcase
61
67
  perm.resource = resource if perm.new_record?
62
- perm.save
63
- result_response << perm
68
+ (perm.new_record?) ? (permissions_to_add << perm) : (permissions_to_update << perm)
64
69
  end
65
70
  end
66
71
  end
67
72
 
68
- (result_response.size == 1) ? result_response.first : result_response
73
+
74
+ unless permissions_to_add.empty?
75
+ attrs = permissions_to_add.first.attributes.keys.collect{ |k| k.to_s }
76
+ sql_insert = "INSERT INTO `permissions` (`#{attrs.join("`,`")}`) VALUES"
77
+ sql_values = []
78
+ permissions_to_add.each do |perm|
79
+ vals = []
80
+ attrs.each{ |a| vals << ((a == "created_at" || a == "updated_at") ? DateTime.now.utc.strftime("%Y-%m-%d %H:%M:%S") : ((a == "id") ? "NULL" : perm.send(a.to_sym))) }
81
+ sql_values << "('#{vals.join("','")}')"
82
+ end
83
+ Permission.connection.execute("#{sql_insert} #{sql_values.join(',')}")
84
+ end
85
+
86
+ unless permissions_to_update.empty?
87
+ permissions_to_update.each do |perm|
88
+ Permission.update_all(['permission_type = ?', perm.permission_type ], ['id = ?', perm.id])
89
+ end
90
+ end
69
91
 
70
92
  end
71
93
 
@@ -100,7 +122,23 @@ module Permissable
100
122
  end
101
123
 
102
124
  def lookup_permissions!
103
- @permissions_cache = PermissionsCache.new(Permission.where(:resource_type => self.class.permissable_resources.collect{ |r| r.to_s.classify }).all)
125
+
126
+ resource_types = self.class.permissable_resources.collect{ |r| r.to_s.classify }
127
+ member_ids = []
128
+ member_types = []
129
+
130
+ resource_types.each do |type|
131
+ identifier = member_identifier(type)
132
+ member_ids << identifier[:member_id]
133
+ member_types << identifier[:member_type]
134
+ end
135
+
136
+ member_ids = member_ids.uniq; member_types = member_types.uniq;
137
+ member_ids = member_ids.first unless member_ids.size > 1
138
+ member_types = member_types.first unless member_types.size > 1
139
+
140
+ @permissions_cache ||= PermissionsCache.new(Permission.where(:member_id => member_ids, :member_type => member_types, :resource_type => resource_types).all)
141
+
104
142
  end
105
143
 
106
144
  private
data/permissable.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{permissable}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brent Kirby"]
12
- s.date = %q{2010-09-13}
12
+ s.date = %q{2010-09-16}
13
13
  s.description = %q{Permissable lets you set a number of 'permissions' to your database models. By assigning 'members' to 'resources' (models) you can specify various permission states.}
14
14
  s.email = %q{brent@kurbmedia.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: permissable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brent Kirby
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-13 00:00:00 -04:00
18
+ date: 2010-09-16 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency