Skip to content
This repository
Browse code

Add example of using dates in the demos

  • Loading branch information...
commit 5c2e7bd9dbe95f9876be48d3952d5b592cc939a0 1 parent f98d5a5
dan hassin authored May 14, 2012
6  extras/rubymotion/app/controllers/responses_view_controller.rb
@@ -77,11 +77,11 @@ def numberOfSectionsInTableView(tableView)
77 77
   end
78 78
   
79 79
   def tableView(tableView, titleForFooterInSection:section)
  80
+    str = ""
80 81
     if @post.responses.empty?
81  
-      "There are no responses to this post.\nSay something!"
82  
-    else
83  
-      nil
  82
+      str += "There are no responses to this post.\nSay something!\n\n"
84 83
     end
  84
+    str += "Posted on #{@post.createdAt.strftime("%m/%d/%y")}"
85 85
   end
86 86
   
87 87
   def tableView(tableView, titleForHeaderInSection:section)
13  extras/rubymotion/app/models/post.rb
... ...
@@ -1,19 +1,22 @@
1 1
 class Post < NSRailsModel
2  
-  attr_writer :author, :content, :responses
  2
+  attr_writer :author, :content, :responses, :createdAt
3 3
 
4 4
   # Right now there's a bug in RubyMotion that requires you to define getter methods manually
5 5
   # Soon you'll be able just do "attr_accessor" above instead of this
6 6
   def author; @author; end  
7 7
   def content; @content; end
8  
-  def responses; @responses; end  
  8
+  def responses; @responses; end
  9
+  def createdAt; @createdAt; end
9 10
   
10 11
    
11 12
   # Define the NSRailsSync macro as usual, only as a class method returning a string
12  
-  # Remember that * is not supported in the Ruby environment, and has_many relationships have to be indicated with '-m'
  13
+  # Remember that in the Ruby environment, * is not supported, has_many relationships have to be indicated with '-m', and dates must declared with NSDate
13 14
   def self.NSRailsSync
14  
-    'author, content, responses:Response -m'
  15
+    'author, content, createdAt:NSDate, responses:Response -m'
15 16
   end
16 17
     
17  
-  # For responses, since it's an array, the ":" is required to define an association with another class.
  18
+  # For responses, since it's an array, -m is required (has-many). The ":" is required to define an association with another class.
18 19
   # In this case, the class of objects we want to fill our responses array with is Response (must be an NSRailsModel subclass)
  20
+  
  21
+  # For createdAt, :NSDate makes it so NSRails will automatically convert to a formatted date object (string) when sending to Rails, and return a Time object when retrieving. Handy.
19 22
 end
14  nsrails/demo/Controllers/ResponsesViewController.m
@@ -131,12 +131,14 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
131 131
 }
132 132
 
133 133
 - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
134  
-{
135  
-	if (post.responses.count == 0)
136  
-	{
137  
-		return @"There are no responses to this post.\nSay something!";
138  
-	}
139  
-	return nil;
  134
+{	
  135
+	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  136
+	[formatter setDateFormat:@"MM/dd/yy"];
  137
+	NSString *timestamp = [@"Posted on " stringByAppendingString:[formatter stringFromDate:post.createdAt]];
  138
+
  139
+	NSString *encouragement = @"There are no responses to this post.\nSay something!\n\n";
  140
+
  141
+	return [NSString stringWithFormat:@"%@%@", (post.responses.count == 0) ? encouragement : @"", timestamp];
140 142
 }
141 143
 
142 144
 - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
1  nsrails/demo/Models/Post.h
@@ -12,5 +12,6 @@
12 12
 
13 13
 @property (nonatomic, strong) NSMutableArray *responses;
14 14
 @property (nonatomic, strong) NSString *author, *content;
  15
+@property (nonatomic, strong) NSDate *createdAt;
15 16
 
16 17
 @end
4  nsrails/demo/Models/Post.m
@@ -9,8 +9,8 @@
9 9
 #import "Post.h"
10 10
 
11 11
 @implementation Post
12  
-@synthesize content, author, responses;
13  
-NSRailsSync(content, author, responses:Response)
  12
+@synthesize content, author, createdAt, responses;
  13
+NSRailsSync(content, author, createdAt, responses:Response)
14 14
 
15 15
 // The NSRailsSync above will tell NSRails to sync up content, author, and responses w/Rails
16 16
 // Could also be done like:

0 notes on commit 5c2e7bd

Please sign in to comment.
Something went wrong with that request. Please try again.