Permalink
Browse files

Add example of using dates in the demos

  • Loading branch information...
1 parent f98d5a5 commit 5c2e7bd9dbe95f9876be48d3952d5b592cc939a0 @dingbat committed May 14, 2012
@@ -77,11 +77,11 @@ def numberOfSectionsInTableView(tableView)
end
def tableView(tableView, titleForFooterInSection:section)
+ str = ""
if @post.responses.empty?
- "There are no responses to this post.\nSay something!"
- else
- nil
+ str += "There are no responses to this post.\nSay something!\n\n"
end
+ str += "Posted on #{@post.createdAt.strftime("%m/%d/%y")}"
end
def tableView(tableView, titleForHeaderInSection:section)
@@ -1,19 +1,22 @@
class Post < NSRailsModel
- attr_writer :author, :content, :responses
+ attr_writer :author, :content, :responses, :createdAt
# Right now there's a bug in RubyMotion that requires you to define getter methods manually
# Soon you'll be able just do "attr_accessor" above instead of this
def author; @author; end
def content; @content; end
- def responses; @responses; end
+ def responses; @responses; end
+ def createdAt; @createdAt; end
# Define the NSRailsSync macro as usual, only as a class method returning a string
- # Remember that * is not supported in the Ruby environment, and has_many relationships have to be indicated with '-m'
+ # Remember that in the Ruby environment, * is not supported, has_many relationships have to be indicated with '-m', and dates must declared with NSDate
def self.NSRailsSync
- 'author, content, responses:Response -m'
+ 'author, content, createdAt:NSDate, responses:Response -m'
end
- # For responses, since it's an array, the ":" is required to define an association with another class.
+ # For responses, since it's an array, -m is required (has-many). The ":" is required to define an association with another class.
# In this case, the class of objects we want to fill our responses array with is Response (must be an NSRailsModel subclass)
+
+ # 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.
end
@@ -131,12 +131,14 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
}
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
-{
- if (post.responses.count == 0)
- {
- return @"There are no responses to this post.\nSay something!";
- }
- return nil;
+{
+ NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+ [formatter setDateFormat:@"MM/dd/yy"];
+ NSString *timestamp = [@"Posted on " stringByAppendingString:[formatter stringFromDate:post.createdAt]];
+
+ NSString *encouragement = @"There are no responses to this post.\nSay something!\n\n";
+
+ return [NSString stringWithFormat:@"%@%@", (post.responses.count == 0) ? encouragement : @"", timestamp];
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
@@ -12,5 +12,6 @@
@property (nonatomic, strong) NSMutableArray *responses;
@property (nonatomic, strong) NSString *author, *content;
+@property (nonatomic, strong) NSDate *createdAt;
@end
@@ -9,8 +9,8 @@
#import "Post.h"
@implementation Post
-@synthesize content, author, responses;
-NSRailsSync(content, author, responses:Response)
+@synthesize content, author, createdAt, responses;
+NSRailsSync(content, author, createdAt, responses:Response)
// The NSRailsSync above will tell NSRails to sync up content, author, and responses w/Rails
// Could also be done like:

0 comments on commit 5c2e7bd

Please sign in to comment.