Sha256: f457ad79fe3adf6e20bde02f5ff6d05a5e6dd06dc11568a4dc380dc947ab841c

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'hashie'
require 'facemock/fb_graph/application/user/permission'

module Facemock
  module FbGraph
    class Application
      class User < Facemock::Database::User
        attr_reader :permission_objects

        def initialize(options={})
          opts = Hashie::Mash.new(options)
          if opts.identifier
            opts[:id] = opts.identifier
            opts.delete(:identifier)
          end
          super(opts)

          @permission_objects = User::Permission.find_all_by_user_id(self.id)
          set_permissions(opts.permissions) if opts.permissions
        end

        def permissions
          @permission_objects.inject([]) do |names, perm|
            names << perm.name.to_sym
          end
        end

        def save!
          super
          @permission_objects.each do |permission|
            permission.save!
          end
        end

        def fetch
          @permission_objects = User::Permission.find_all_by_user_id(self.id)
          super
        end

        def destroy
          @permission_objects.each do |permission|
            permission.destroy
          end
          super
        end

        def revoke!
          @permission_objects.each do |permission|
            permission.destroy
          end
          @permission_objects = []
        end

        private

        def set_permissions(permissions_string)
          permissions_string.gsub(/\s/, "").split(",").uniq.each do |permission_name|
            unless @permission_objects.find{|perm| perm.name == permission_name}
              @permission_objects << User::Permission.new(
                name: permission_name,
                user_id: self.id
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facemock-0.0.6 lib/facemock/fb_graph/application/user.rb
facemock-0.0.5 lib/facemock/fb_graph/application/user.rb