Class: BookController

Inherits:
Formotion::FormController
  • Object
show all
Defined in:
app/controllers/book_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



2
3
4
# File 'app/controllers/book_controller.rb', line 2

def _id
  @_id
end

#meteorObject

Returns the value of attribute meteor.



2
3
4
# File 'app/controllers/book_controller.rb', line 2

def meteor
  @meteor
end

Instance Method Details

#handle_remove(action, result) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/book_controller.rb', line 73

def handle_remove action, result
	if action == :error
		alert = UIAlertView.new
 		alert.message = "Error: #{result['reason'].to_s}"
 		alert.addButtonWithTitle "OK"
 		alert.show
	elsif action == :result
		alert = UIAlertView.new
 		alert.message = "Book removed sucessfully"
 		alert.addButtonWithTitle "OK"
 		alert.show
	end
end

#handle_update(action, result) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/book_controller.rb', line 52

def handle_update action, result
	if action == :error
		alert = UIAlertView.new
 		alert.message = "Error: #{result['reason'].to_s}"
 		alert.addButtonWithTitle "OK"
 		alert.show
	elsif action == :result
		alert = UIAlertView.new
		if @_id != nil
 			alert.message = "Book updated sucessfully"
 			alert.addButtonWithTitle "OK"
 			alert.show	
 		else
 			alert.message = "Book added sucessfully"
 			alert.addButtonWithTitle "OK"
 			alert.show	
 			self.navigationController.popViewControllerAnimated(true)
 		end
	end
end

#removeObject



88
89
90
# File 'app/controllers/book_controller.rb', line 88

def remove
	@meteor.call('/books/remove', self.method(:handle_remove), [{_id: @_id}])
end

#update_form(action, id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/book_controller.rb', line 39

def update_form action, id
	if id == @_id
		if action == :removed
			self.navigationController.popViewControllerAnimated(true)
		elsif action == :changed
			book = @books.find(id)
			self.form.row(:title).value = book['title']
			self.form.row(:author).value = book['author']
			self.form.row(:year).value = book['year']
		end
	end
end

#viewDidLoadObject



4
5
6
7
# File 'app/controllers/book_controller.rb', line 4

def viewDidLoad
	super
	self.title = "Details"
end

#viewWillAppear(animated) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/book_controller.rb', line 9

def viewWillAppear animated
super
@books = @meteor.collections['books']
  @books.add_observer( self.method(:update_form) )

  self.form.on_submit do |form|
	data = form.render
	self.view.endEditing(true)

	if @_id != nil
		@meteor.call('/books/update', self.method(:handle_update), [{_id: @_id},{title: data[:title], author: data[:author],year: data[:year]}])
	else
		new_id = SecureRandom.hex
		@meteor.call('/books/insert', self.method(:handle_update), [{_id: new_id,title: data[:title], author: data[:author],year: data[:year]}])
	end

end

if @_id != nil
	right_button = UIBarButtonItem.alloc.initWithTitle("Remove", style: UIBarButtonItemStyleDone, target:self, action:'remove')
 	self.navigationItem.rightBarButtonItem = right_button
 end
end

#viewWillDisappear(animated) ⇒ Object



33
34
35
36
# File 'app/controllers/book_controller.rb', line 33

def viewWillDisappear animated
	super
	@books.remove_observer( self.method(:update_form) )
end