Skip to content

Instantly share code, notes, and snippets.

@Papillard
Papillard / presenter.rb
Created July 17, 2013 14:19
Use presenter if the controller is full of instance variables. Memoize instance variables !
# => config/application.rb
config.autoload_paths = %W(#{config.route}/presenters) # loading the file defining the presenter
#/app/presenters/tweets/show_presenter
module Tweets
class ShowPresenter
extend ActiveSupport::Memoizable # Module of class instance methods : memorize instante variables
def initialize(tweet)
@tweet = tweet
@Papillard
Papillard / custom_url.rb
Created July 17, 2013 14:20
custom urls over-riding ActiveRecord::Base#to_param
# Overload to_param method to custom urls
# Tweet.find(params[:id]) will still work !
class Tweet < ActiveRecord::Base
belongs_to :user
def to_param
"#{id}-#{user.username}" # => url will be /76-Papillard
end
end
@Papillard
Papillard / activerecord_like_model.rb
Created July 17, 2013 14:23
include ActiveModel mixin to use ActiveRecord validations & helpers
# Including ActiveModel validations & conversion
class SupportRequest
include ActiveModel::Validations
include ActiveModel::Conversion
attr_accessor :name, :email, :problem
validates_presence_of :name, :email, :problem
def initialize(attributes={})
self.name = attributes[:name]
@Papillard
Papillard / with_scope.rb
Created July 17, 2013 14:24
scope your frequent requests
# Final
class Following < ActiveRecord::Base
$scope :recent, lambda { where(:user_id => current_user.id).where(['created_at > ?', 2.days.ago]) }
end
class UserController < ApplicationController
def index
@followings = current_user.followings.recent
end
end
@Papillard
Papillard / with_callback.rb
Created July 17, 2013 14:26
Use callbacks to make your controller concise
# More readable controller where "hot topic" logic delegate to model callback
class TopicsController < ApplicationController
def create
@topic = Topic.new(params[:topic])
if @topic.save
redirect_to @topic, :notice => 'Successfully created a Tweet'
else
render :new
end
end
@Papillard
Papillard / with_nested_attributes.rb
Created July 17, 2013 14:27
Use nested attributes to avoid repeating validation and save
# Using nested attribute
class Tweet < ActiveRecord::Base
has_one :location, :dependent => :destroy
accepts_nested_attributes_for :location
end
class TweetsController < ApplicationController
def new
@tweet = current_user.tweets.new(:location => Location.new) # need to instantiate the Location object
end
@Papillard
Papillard / N+1.rb
Last active December 19, 2015 21:38
Query optimization !
# Initial
# N+1 query : 1. getting the tweet's favorites array
# 2. for N favorites, get the associated user
class Tweet < ActiveRecord::Base
has_many :favorites
def favorited_users
self.favorites.collect {|fav| fav.user }
end
end
@Papillard
Papillard / index.html.erb
Last active December 19, 2015 21:39
DB seed for food blog !
<% @posts.each do |post| %>
<h1><%= post.name %></h1>
<p><%= post.content%></p>
<%= image_tag post.picture_url %>
<% end %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JPDS</title>
<!--Bootstrap CSS-->
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<!--FontAwesome-->
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet">
<!--Your CSS-->
body{
padding-top: 100px;
font-family: 'Open Sans', sans-serif;
background: rgb(239,237,222);
}
/* custom links */
a:hover{
text-decoration: none;
}