This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# => 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @posts.each do |post| %> | |
<h1><%= post.name %></h1> | |
<p><%= post.content%></p> | |
<%= image_tag post.picture_url %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body{ | |
padding-top: 100px; | |
font-family: 'Open Sans', sans-serif; | |
background: rgb(239,237,222); | |
} | |
/* custom links */ | |
a:hover{ | |
text-decoration: none; | |
} |