Monday, March 24, 2014

Paper Clip Gem

Paper Clip Gem - Review

https://github.com/thoughtbot/paperclip


Paperclip is a gem used for attaching to Rails applications.

class UserPhoto < ActiveRecord::Base
belongs_to :user
has_attached_file :photo, :styles =>
{ :medium => "300>x300", :thumb => "200x200#" },
:default_url => "assets/images/:style/missing.png"
validates :user_id, presence: true
validates :caption, length: { maximum: 140 }
validates :photo_width, presence: true
validates :photo_height, presence: true
# Validations for Photo's
validates :photo, presence: true
# Don't let people upload Giant phot's and charge alot of out AWS account
validates_attachment_size :photo, :less_than => 5.megabytes
# Only let people uplad a photo
validates_attachment_content_type :photo,
:content_type => ['image/jpeg', 'image/png', 'image/jpg']
after_post_process :save_image_dimensions
def save_image_dimensions
geo = Paperclip::Geometry.from_file(photo.queued_for_write[:original])
self.photo_width = geo.width.to_i
self.photo_height = geo.height.to_i
end
end
view raw gistfile1.rb hosted with ❤ by GitHub
Show View
<%= image_tag @user.user_photo.url %>
<%= image_tag @user.user_photo.url(:medium) %>
<%= image_tag @user.user_photo.url(:thumb) %>

Important Finds

:thumb => "200x200#"
The # signifies that this image is going to be saved as as a square on the inside of the photo. This is very useful for user profile pictures.

:medium => "300>x300"
This ensures that the image will have a a width greater than 300 px. 


Profile Pictures

User has_many UserPhotos

- I started by saving a bolean field on the UserPhoto to check if there is a default photo.
- I later found out that it is better to save a link on the user model

class AddDefaultPhotoIdToUsers < ActiveRecord::Migration
def change
add_column :users, :default_photo_id, :integer
remove_column :user_photos, :default_photo
end
end
view raw gistfile1.rb hosted with ❤ by GitHub
def get_default_photo
self.user_photos.find_by_id(default_photo_id)
end
view raw gistfile1.rb hosted with ❤ by GitHub


How to Link to AWS Account

- It is a a bad idea to save images to the project. Amazon Web Server and Heroku work well together in order to save files at a cheap fast cost.

Monday, March 10, 2014

Class Instance Variables

Use Class instance Variables

We all know that class variables such as @@ are dangerous to use due to their leaky inheritance.

class Presentaion < Document
@@default_font = :ariel
def self.default_font=(font)
@@default_font=(font)
end
def default_font
@@default_font
end
end
class Resume < Document
@@default_font = :nimbus
def self.default_font=(font)
@@default_font=(font)
end
def default_font
@@default_font
end
end
class Document
@@default_font = :times ##LEAKY INTHERITANCE HERE
end
require 'document'
require 'resume' # Rewrite class variable
require 'presentation' # Rewrite class viable
##THE FIX
# As an alternative we can use...
# 1. Use class variables with this in mind
# 2. Use CONSTANTS
# 3. Open up the Singleton Class
class Presentaion < Document
@default_font = :ariel
class << self
attr_accessor :default_font
end
end
class Resume < Document
@default_font = :nimbus
class << self
attr_accessor :default_font
end
end
class Document
@default_font = :times
class << self
attr_accessor :default_font
end
end
view raw gistfile1.rb hosted with ❤ by GitHub

<script src="https://gist.github.com/surgentt/9464842.js"></script>class Presentaion < Document


WHAT IS GOING ON HERE

 The class << self syntax opens up a classes singleton class.
  This allows you to specialize the behavior of methods called on that specific object.

http://stackoverflow.com/questions/2505067/class-self-idiom-in-ruby

Object
    ^
     |
  <Singleton Class>
    ^
    |
Instance

Singleton Method is a method that is defined for exactly one object instance.


The <Singleton Class> is only invoked when it is filled with something.

  - An example of this is a method on an instance variable.


hand_built_stub_printer = Object.new
def hand_built_stub_printer.avaiable?
true
end