Module: Appoxy::Sessions::Shareable

Defined in:
lib/sessions/shareable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_results(which, q) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/sessions/shareable.rb', line 166

def self.get_results(which, q)
    @sdb = SimpleRecord::Base.connection
    next_token = nil
    ret = []
    begin
        begin
            response =  @sdb.select(q, next_token)
            rs = response[:items]
            rs.each_with_index do |i, index|
                puts 'i=' + i.inspect
                i.each_key do |k|
                    puts 'key=' + k.inspect
                    if which == :first
                        return i[k].update("id"=>k)
                    end
                    ret << i[k]
                end
#    break if index > 100
            end
            next_token = response[:next_token]
        end until next_token.nil?
    rescue Aws::AwsError, Aws::ActiveSdb::ActiveSdbError
        if ($!.message().index("NoSuchDomain") != nil)
            puts 'NO SUCH DOMAIN!!!'
            # this is ok
        else
            raise $!
        end
    end
    which == :first ? nil : ret
end

Instance Method Details

#common_attributesObject



80
81
82
# File 'lib/sessions/shareable.rb', line 80

def common_attributes
    ["new_share", "id", "created", "updated", "user_id", item_id_name]
end

#item_id_nameObject



76
77
78
# File 'lib/sessions/shareable.rb', line 76

def item_id_name
    return self.class.name.foreign_key
end

#share_domainObject



156
157
158
159
160
161
162
163
# File 'lib/sessions/shareable.rb', line 156

def share_domain
#                puts 'instance share_domain'
    ret = self.class.name + "User"
#                puts 'SHARE_NAME=' + ret
    ret = ret.tableize
#                puts 'ret=' + ret
    ret
end

#share_id(user) ⇒ Object



152
153
154
# File 'lib/sessions/shareable.rb', line 152

def share_id(user)
    "#{self.id}_#{user.id}"
end

#share_with(email, access_rights = {}, options = {}) ⇒ Object

Call this method on your Sharable object to share it with the person. returns: a hash with :user (the user that the item was shared with), :ac (activation code that should be sent to the user)

or false if couldn't be shared.

You can check for errors by looking at the errors array of the object. Eg:

if my_ob.share_with(x)
   # all good
   Mail the user a link that contains user_id and ac, this gem will take care of the rest.
else
   # not all good, check errors
   errors = my_ob.errors
end


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sessions/shareable.rb', line 18

def share_with(email, access_rights={}, options={})

    access_rights = {} if access_rights.nil?

    @email = email.strip

    if @email == self.user.email
        self.errors.add_to_base("User already owns this item.")
        return false
    end

    user = ::User.find_by_email(@email)
    if user.nil?
        # lets create the user and send them an invite.
        user = ::User.new(:email=>@email, :status=>"invited")
        user.set_activation_code # todo: this shouldn't be on user anymore
        if user.save

        else
            self.errors = user.errors
            return false
        end
    end
    activation_code = user.activation_code

    # check if exists
    share_domain = self.share_domain
    item_id_name = self.item_id_name
#                puts 'share_domain = ' + share_domain.inspect
    @sdb = SimpleRecord::Base.connection
#        @shared_with = share_class.find(:first, :conditions=>["user_id = ? and item_id = ?", user.id, @item.id])
    @project_user = Shareable.get_results(:first, ["select * from #{share_domain} where user_id=? and #{item_id_name} = ?", user.id, self.id])
    puts 'sharing user=' + @project_user.inspect
    unless @project_user.nil?
        self.errors.add_to_base("This item is already shared with #{email}.")
        return false
    end

    now = Time.now
    id = share_id(user)
    @sdb.put_attributes(share_domain, id, {:new_share=>true,
                                           :id=>id,
                                           :created=>SimpleRecord::Translations.pad_and_offset(now),
                                           :updated=>SimpleRecord::Translations.pad_and_offset(now),
                                           :user_id => user.id,
                                           :activation_code=>activation_code,
                                           :status=>"invited",
                                           item_id_name => self.id}.merge(access_rights), true)

#                ret = {
#                        :user=>user,
#                        :ac=>activation_code
#                }
#                return ret
    return user

end

#shared_withObject

Returns a list of users that this item is shared with.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sessions/shareable.rb', line 85

def shared_with
    project_users = Shareable.get_results(:all, ["select * from #{share_domain} where #{item_id_name} = ?", self.id])
    user_ids = []
    options_hash = {}
    project_users.each do |puhash|
        puhash.each_pair do |k, v|
            puhash[k] = v[0]
        end
        puts 'puhash=' + puhash.inspect
        user_ids << puhash["user_id"]
        options_hash[puhash["user_id"]] = puhash
    end
    ret = ::User.find(:all, :conditions=>["id in ('#{user_ids.join("','")}')"]).collect do |u|
        def u.share_options=(options=nil)
            instance_variable_set(:@share_options, options)
        end

        def u.share_options
            instance_variable_get(:@share_options)
        end

        u.share_options=options_hash[u.id]
        u
    end
    ret
end

#unshare(user) ⇒ Object

Unshare by user.



123
124
125
126
127
128
129
130
# File 'lib/sessions/shareable.rb', line 123

def unshare(user)
    @sdb = SimpleRecord::Base.connection
    @sdb.delete_attributes(share_domain, share_id(user))
#                @project_user = Shareable.get_results(:first, ["select * from #{share_domain} where user_id=? and #{item_id_name} = ?", user.id, self.id])
#                @project_user.each do |pu|
#                    @sdb.delete_attributes(share_domain, pu["id"])
#                end
end

#unshare_by_id(id) ⇒ Object

this unshares by the



113
114
115
116
117
118
119
120
# File 'lib/sessions/shareable.rb', line 113

def unshare_by_id(id)
#        @project_user = ProjectUser.find(params[:pu_id])
#        @project_user.delete
#                puts 'unsharing ' + id.to_s
    @sdb = SimpleRecord::Base.connection
    puts "delete_attributes=" + @sdb.delete_attributes(share_domain, id.to_s).inspect
#                puts 'deleted?'
end

#update_sharing_options(user, options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sessions/shareable.rb', line 132

def update_sharing_options(user, options={})
    options={} if options.nil?
#                puts 'options=' + ({ :updated=>Time.now }.merge(options)).inspect
    @sdb = SimpleRecord::Base.connection
    @project_user = Shareable.get_results(:first, ["select * from #{share_domain} where user_id=? and #{item_id_name} = ?", user.id, self.id])
    # compare values
    to_delete = []
    @project_user.each_pair do |k, v|
        if !common_attributes.include?(k) && !options.include?(k)
            to_delete << k
        end
    end
    if to_delete.size > 0
        puts 'to_delete=' + to_delete.inspect
        @sdb.delete_attributes(share_domain, share_id(user), to_delete)
    end
    @sdb.put_attributes(share_domain, share_id(user), {:updated=>Time.now}.merge(options), true)

end