Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Add example of using dates in the demos
Browse files Browse the repository at this point in the history
  • Loading branch information
dingbat committed May 14, 2012
1 parent f98d5a5 commit 5c2e7bd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions extras/rubymotion/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 8 additions & 6 deletions nsrails/demo/Controllers/ResponsesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions nsrails/demo/Models/Post.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

@property (nonatomic, strong) NSMutableArray *responses;
@property (nonatomic, strong) NSString *author, *content;
@property (nonatomic, strong) NSDate *createdAt;

@end
4 changes: 2 additions & 2 deletions nsrails/demo/Models/Post.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5c2e7bd

Please sign in to comment.