lib/fabric/map.rb in fabric-0.2.0 vs lib/fabric/map.rb in fabric-0.3.0
- old
+ new
@@ -61,14 +61,24 @@
user = self.users.create(:name => name.to_s)
self.load_keys_from_repository(user)
end
end
- def grant(user_opts, role_opts, opts = {})
- users = load_by_attribute(:users, :name, user_opts)
- roles = load_by_attribute(:roles, :name, role_opts)
+ # This method has become too long again...
+ def grant(user_names, role_names, role_conditions = {})
+ users = self.users.all
+ roles = self.roles.all
+ users = users.all(:name => user_names) unless user_names == :all
+ roles = roles.all(:name => role_names) unless role_names == :all
+
+ if role_conditions.include?(:except)
+ roles = roles.all(:name.not => role_conditions[:except])
+ end
+
+ raise DataMapper::ObjectNotFoundError if users.empty? or roles.empty?
+
roles.each do |role|
role.users += users
end
end
@@ -81,22 +91,8 @@
path = File.join(self.expanded_key_repository_path, "#{user.name}.pub")
narrate "Looking for key for #{user.name} in #{path}"
if File.exists?(path)
user.keys.create(:public_key => File.read(path))
- end
- end
-
- def load_association(klass, opts = {})
- association = self.send(klass.to_sym).all(opts)
- raise DataMapper::ObjectNotFoundError if association.empty?
- association
- end
-
- def load_by_attribute(klass, attribute, opts = {})
- if opts == :all
- load_association(klass)
- else
- load_association(klass, attribute.to_sym => opts)
end
end
end
\ No newline at end of file