Browse code

Prihlasovacie udaje o pouzivatelovi vo vlastnej triede

Cinan Rakosnik authored on 12/05/2013 at 19:15:31
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+class Account
1
+	include Mongoid::Document
2
+
3
+    field :jid, type: String
4
+    field :pass, type: String
5
+
6
+    embedded_in :user
7
+end
0 8
\ No newline at end of file
... ...
@@ -1,10 +1,13 @@
1 1
 class User
2 2
 	include Mongoid::Document
3 3
 
4
-	field :jids, type: Array
4
+    embeds_many :accounts
5
+    accepts_nested_attributes_for :accounts
6
+
7
+    index({ 'accounts.jid' => 1 })
5 8
 
6 9
     def update_pass(jid, pass)
7
-        account_credentials = jids.detect do |f|
10
+        account_credentials = accounts.detect do |f|
8 11
             f[:jid] == jid || f['jid'] == jid
9 12
         end
10 13
 
... ...
@@ -13,17 +16,18 @@ class User
13 13
     end
14 14
 
15 15
     def self.existing_jid(jid)
16
-        where('jids.jid' => jid).only(:jids).first
16
+        where('accounts.jid' => jid).only(:accounts).first
17 17
     end
18 18
 
19 19
     def self.create_jid(jid)
20
-        new_user = new(jids: [ {jid: jid} ])
20
+        new_user = new()
21
+        new_user.add_account(jid)
21 22
         new_user.save
22 23
         new_user
23 24
     end
24 25
 
25
-    def add_account(another_jid, password)
26
-        jids << {jid: another_jid, pass: password}
26
+    def add_account(another_jid, password = nil)
27
+        accounts << Account.new(jid: another_jid, pass: password)
27 28
         save
28 29
     end
29 30
 
... ...
@@ -35,7 +39,11 @@ class User
35 35
             return []
36 36
         end
37 37
 
38
-        user = find(user_id)
39
-        return user ? user.jids : []
38
+        user = where(id: user_id).only(:accounts).first
39
+        return user ? user.accounts : []
40
+    end
41
+
42
+    def self.can_save_conversation(my_jid, friend_jid, chat_id = nil)
43
+
40 44
     end
41 45
 end
42 46
\ No newline at end of file