Permalink
Browse files

add example app for using Formable model

  • Loading branch information...
1 parent 95862e1 commit 8227cbb4f58b2525d2746d9b3575b7b0ec4b7d44 @clayallsopp committed Aug 3, 2012
@@ -0,0 +1,5 @@
+.repl_history
+build
+resources/*.nib
+resources/*.momd
+resources/*.storyboardc
@@ -0,0 +1,9 @@
+# -*- coding: utf-8 -*-
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+require '../../lib/formotion'
+
+Motion::Project::App.setup do |app|
+ # Use `rake config' to see complete project settings.
+ app.name = 'FormModel'
+end
@@ -0,0 +1,11 @@
+class AppDelegate
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
+
+ @navigation_controller = UINavigationController.alloc.initWithRootViewController(UsersController.alloc.initWithNibName(nil, bundle:nil))
+ @window.rootViewController = @navigation_controller
+ @window.makeKeyAndVisible
+
+ true
+ end
+end
@@ -0,0 +1,18 @@
+class User
+ include Formotion::Formable
+
+ attr_accessor :name, :score, :team
+
+ form_property :name, :string
+ form_property :score, :number, transform: lambda { |value| value.to_i }
+
+ form_property :team, :picker, items: ["Red", "Blue", "Green"]
+
+ form_title "Edit User"
+
+ def initialize(name, score, team)
+ self.name = name
+ self.score = score
+ self.team = team
+ end
+end
@@ -0,0 +1,52 @@
+class UsersController < UITableViewController
+ def users
+ @users ||= [User.new("Harry", 100, "Green"),
+ User.new("Ron", 80, "Blue"),
+ User.new("Hermione", 120, "Red")]
+ end
+
+ def viewDidLoad
+ super
+ self.title = "Scoreboard"
+ end
+
+ def viewDidAppear(animated)
+ super
+ self.users.sort_by! { |x| x.score }.reverse!
+ self.tableView.reloadData
+ end
+
+ def tableView(tableView, numberOfRowsInSection:section)
+ self.users.count
+ end
+
+ def tableView(tableView, cellForRowAtIndexPath:indexPath)
+ @identifier ||= "IDENTIFIER"
+
+ cell = tableView.dequeueReusableCellWithIdentifier(@indentifier) || begin
+ UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:@identifier)
+ end
+
+ user = self.users[indexPath.row]
+ cell.textLabel.text = "##{indexPath.row + 1}: #{user.name}"
+ cell.detailTextLabel.text = user.team
+ cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
+
+ cell
+ end
+
+ def tableView(tableView, didSelectRowAtIndexPath:indexPath)
+ tableView.deselectRowAtIndexPath(indexPath, animated:true)
+
+ user = self.users[indexPath.row]
+ alert = UIAlertView.alloc.initWithTitle(user.name, message:"Score: #{user.score}", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:nil)
+ alert.show
+ end
+
+ def tableView(tableView, accessoryButtonTappedForRowWithIndexPath:indexPath)
+ # WHERE THE MAGIC HAPPENS!
+ user = self.users[indexPath.row]
+ controller = Formotion::FormableController.alloc.initWithModel(user)
+ self.navigationController.pushViewController(controller, animated:true)
+ end
+end
@@ -0,0 +1,9 @@
+describe "Application 'FormModel'" do
+ before do
+ @app = UIApplication.sharedApplication
+ end
+
+ it "has one window" do
+ @app.windows.size.should == 1
+ end
+end

0 comments on commit 8227cbb

Please sign in to comment.