Skip to content

Instantly share code, notes, and snippets.

@buk
Created November 21, 2012 07:48
Show Gist options
  • Save buk/4123672 to your computer and use it in GitHub Desktop.
Save buk/4123672 to your computer and use it in GitHub Desktop.
The Participant Model
class ApiParticipant
def self.fetch_participants(&block)
BubbleWrap::HTTP.get("http://jugendgruppe-backend.192.168.2.105.xip.io/participants.json") do |response|
if response.ok?
json = BubbleWrap::JSON.parse(response.body)
participants = json.map {|pj| Participant.from_json(pj["participant"])}
p "You are here: #{self.name}: #{participants}"
block.call(true, participants)
else
block.call(false, nil)
end
end
end
end
class Participant
attr_accessor :first_name,
:last_name,
:date_of_birth,
:group,
:swimmer
def initialize(attrs)
attrs.each_pair do |key, value|
self.send("#{key}=", value)
end
end
def self.from_json(json)
new(:first_name => json["first_name"])
end
# def self.last_name_from_json(json)
# new(:last_name => json["last_name"])
# end
# def self.date_of_birth_from_json(json)
# new(:date_of_birth => json["date_of_birth"])
# end
# def self.group_from_json(json)
# new(:group => json["group"])
# end
# def self.swimmer_from_json(json)
# new(:swimmer => json["swimmer"])
# end
end
[{"participant":{"allergy":"Lactose Intolerant","created_at":"2012-11-20T05:40:07Z","date_of_birth":"2007-10-06","emergency_number":"123456","first_name":"Sebastian","group":"St. Peter","health_insurance_card":true,"id":1,"last_name":"B","swimmer":true,"updated_at":"2012-11-20T05:40:07Z","vaccination_certificate":true}},{"participant":{"allergy":"Fructose Intolerant","created_at":"2012-11-20T05:41:17Z","date_of_birth":"2008-03-03","emergency_number":"123456","first_name":"Johannes ","group":"St. Johannes","health_insurance_card":true,"id":2,"last_name":"B","swimmer":true,"updated_at":"2012-11-20T05:41:17Z","vaccination_certificate":true}}]
class Participants_detailTableViewController < UITableViewController
def viewDidLoad
super
end
def viewDidUnload
super
end
# helper methods
def bind_with_participants(participant)
@participant = participant
navigationItem.title = participant.first_name
view.reloadData
end
def shouldAutorotateToInterfaceOrientation(interfaceOrientation)
interfaceOrientation == UIInterfaceOrientationPortrait
end
## Table view data source
def numberOfSectionsInTableView(tableView)
1
end
def tableView(tableView, numberOfRowsInSection:section)
5
end
def tableView(tableView, cellForRowAtIndexPath:indexPath)
cellIdentifier = self.class.name
cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) || begin
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:cellIdentifier)
cell
end
# first_name = @participant['first_name'][indexPath.row]
# p "#{self.name} #{first_name}"
# cell.textLabel.text = first_name
# cell
end
## Table view delegate
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
end
end
class ParticipantsTableViewController < UITableViewController
def viewDidLoad
super
@participants ||= []
ApiParticipant.fetch_participants do |success, participants|
if success
@participants = participants
p "Received #{@participants.length} participants"
self.tableView.reloadData
else
App.alert("Oops!")
end
end
self.navigationItem.title = "Teilnehmer"
end
def viewDidUnload
super
end
def shouldAutorotateToInterfaceOrientation(interfaceOrientation)
interfaceOrientation == UIInterfaceOrientationPortrait
end
## Table view data source
def numberOfSectionsInTableView(tableView)
# Return the number of sections.
1
end
def tableView(tableView, numberOfRowsInSection:section)
# Return the number of rows in the section.
# @participants ? @participants.length : 0
@participants.length
end
def tableView(tableView, cellForRowAtIndexPath:indexPath)
cellIdentifier = self.class.name
cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) || begin
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:cellIdentifier)
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
cell
end
participant = @participants[indexPath.row]
cell.textLabel.text = participant.first_name
cell
end
## Table view delegate
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
participants_detail_table_view_controller = Participants_detailTableViewController.alloc.init
self.navigationController.pushViewController(participants_detail_table_view_controller, animated:true)
participant = @participants[indexPath.row]
participants_detail_table_view_controller.bind_with_participants(participant)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment