Skip to content

Instantly share code, notes, and snippets.

@Sunnyztj
Sunnyztj / gist:7816517
Created December 6, 2013 00:13
FactoryGirl
FactoryGirl.define do
factory :prize do
name "iPhone"
image { File.new(Rails.root.join("app/assets/images/sample/sample-prize-2.png")) }
icon { File.new(Rails.root.join("app/assets/images/sample/scratch-2.png")) }
end
end
rails g model Coupon name coupon_type discount:float description:text category_id:integer address:string terms_and_conditions:text
t.string :image_file_name
t.integer :image_file_size
t.string :image_content_type
@Sunnyztj
Sunnyztj / gist:8049825
Last active December 31, 2015 21:49
update database in rails
tootz git:(develop) rails c
[1] pry(main)> User.where("email like '%webztj%'")
[2] pry(main)> u = User.find 1
[3] pry(main)> Ticket.create user_id: 1
[4] pry(main)> Message.create user_id: 2, subject: 'hello wolrd', body: 'good day'
reload!
Ticket.all.each{|t| t.assign_images }
u = User.find 1
@Sunnyztj
Sunnyztj / gist:8277208
Created January 6, 2014 02:19
Objective C connect with Rails servers
- (NSDictionary*) fetchMessageList:(NSInteger)pagenum {
NSString* urlstring = [NSString stringWithFormat:@"%@/api/v1/user/messages.json?page=%d", BaseURL, pagenum];
NSLog(@"Message List URL : %@", urlstring);
NSURL* url = [NSURL URLWithString:urlstring];
MBGetDataRequest* request = [MBGetDataRequest requestWithURL:url];
[request applyHTTPHeaders:[NSDictionary dictionaryWithObjectsAndKeys:
[[MBPreferenceManager sharedPreferenceManager] getSecurityKey],
@"Http-X-User-Access-Token",
@Sunnyztj
Sunnyztj / gist:8277612
Created January 6, 2014 03:06
QuickDialog add a object to QSelectSection
QSection *sec2 = [[[QSection alloc] initWithTitle:@""] autorelease];
[root addSection:sec2];
QButtonElement *buttonElement1 = [[QButtonElement alloc] initWithTitle:@"Done"];
buttonElement1.key = @"Done";
buttonElement1.onSelected = ^(void) {
NSMutableDictionary* d = [[NSMutableDictionary alloc] init];
[root fetchValueIntoObject:d];
QSelectSection* sec = (QSelectSection*)[self.root sectionWithKey:@"Addresses"];
@Sunnyztj
Sunnyztj / gist:8345960
Created January 10, 2014 02:24
change UITextField
- (void) textViewDidEndEditing:(UITextView *)textView
{
CGRect frame = mTextView.frame;
frame.size.height = mTextView.contentSize.height;
mTextView.frame = frame;
}
@Sunnyztj
Sunnyztj / gist:8346610
Created January 10, 2014 03:36
rails s api
1. update:
api:
put :update do
authenticate!
if current_user.update_attributes params[:user]
current_user
else
error!({ error: current_user.errors.full_messages }, 422)
@Sunnyztj
Sunnyztj / gist:8449842
Created January 16, 2014 04:36
ios UITableView
1. get specify cell in UITableView:
UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
@Sunnyztj
Sunnyztj / gist:8535173
Created January 21, 2014 06:14
sort in objective c (Block)
NSArray* ary = [dict objectForKey:@"stores"];
NSArray* sortedArray = [[NSArray alloc] init];
sortedArray = [ary sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *first = [NSString stringWithFormat:@"%@", [obj1 objectForKey:@"distance"]];
NSString *second = [NSString stringWithFormat:@"%@", [obj2 objectForKey:@"distance"]];
NSString *comFirst = [first substringWithRange:NSMakeRange(first.length-2, 1)];
NSString *comSecond = [second substringWithRange:NSMakeRange(second.length-2, 1)];
NSString *firstfloat;
NSString *seconfloat;
@Sunnyztj
Sunnyztj / gist:8682269
Created January 29, 2014 05:21
perfect code in ROR
One. coupon_id => coupon.business => business.stores
1.{
get '/:id', rabl: 'coupon' do
authenticate!
@coupon = Coupon.find params[:id]
end
object @coupon
attributes :id, :name, :category, :business_name, :expired_at