You are on page 1of 20

EPE DESARROLLO PARA ENTORNO WEB

Unidad 4 | Capa de Persistencia

CARACTERÍSTICAS & GEMAS


LOGRO
Al finalizar la unidad el alumno
desarrolla la capa de negocio de una
aplicación Web.
AGENDA
ATTACHMENTS
TAGS
AGENDA
ATTACHMENTS
TAGS
PAPERCLIP
https://github.com/thoughtbot/paperclip

#Instalar requisitos
$ sudo apt-get install imagemagick --fix-missing

# En Gemfile
gem "paperclip", "~> 4.2"
PAPERCLIP | AGREGAR A MODELO
# Agregar avatar a User
$ rails g paperclip user avatar

# Migración
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def up
add_attachment :users, :avatar
# Adiciona las columnas:
# avatar_file_name
# avatar_file_size
# avatar_content_type
# avatar_updated_at
end

def down
remove_attachment :users, :avatar
end
end
PAPERCLIP | CONFIGURAR EN MODELO
class User < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :medium => "300x300>",
:thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
end
PAPERCLIP | VALIDAR EN MODELO
class User < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :medium => "300x300>",
:thumb => "100x100>" },
:default_url => "/images/:style/missing.png"

# Validate presence
validates :avatar, :attachment_presence => true

# Validate content type


validates_attachment_content_type :avatar,
:content_type => /\Aimage\/.*\Z/

# Validate two or more conditions


validates_attachment :avatar, :presence => true,
:content_type => { :content_type => "image/jpeg" },
:size => { :in => 0..10.kilobytes }

end
PAPERCLIP | VALIDAR EN MODELO
class User < ActiveRecord::Base
has_attached_file :avatar,
styles: {medium: "300x300>",
thumb: "100x100>" },
default_url: "/images/:style/missing.png"

# Validate presence
validates :avatar, attachment_presence: true, presence: true
# Validate two or more conditions
validates_attachment :avatar,
presence: true,
content_type:
{content_type:
["image/jpeg", "image/gif", "image/png"]
},
size: { in: 0..10.kilobytes }

end
PAPERCLIP | NO SÓLO IMÁGENES
class Product < ActiveRecord::Base
has_attached_file :guide,
styles: {thumbnail: "60x60#"}
validates :guide, presence: true

validates_attachment :guide,
presence: true,
content_type:
{content_type: "application/pdf" }

end
PAPERCLIP | REGISTRO
<%= simple_form_for @user, html: {multipart: true} do |f| %>
<%= f.input :avatar, as: :file %>
<% end %>
PAPERCLIP | VISUALIZACIÓN
<%= image_tag @user.avatar.url(:medium) %>

# ó

<%= image_tag @user.avatar.url(:thumb) %>


AGENDA
ATTACHMENTS
TAGS
ACT_AS_TAGGABLE_ON
https://github.com/mbleigh/acts-as-taggable-on
# En Gemfile
gem 'acts-as-taggable-on', '~> 3.4'

$ bundle

$ rake acts_as_taggable_on_engine:install:migrations

$ rake db:migrate

$ rake acts_as_taggable_on_engine:tag_names:collate_bin
ACT_AS_TAGGABLE_ON | MODELO
class Product < ActiveRecord::Base
act_as_taggable # act_as_tagable_on :tags
end

class Customer < ActiveRecord::Base


act_as_taggable_on :hobbies
end
ACT_AS_TAGGABLE_ON | CONTROLLER
class ProductsController < ApplicationController
# …
def product_params
params.require(:product).permit(:name, :description, :tag_list)
end
end
ACT_AS_TAGGABLE_ON | USO
@product.tag_list.add("shoes", "sport", "black")
@product.tag_list.remove("sport")

@customer.hobby_list.add("basket", "movies", "photography")

@customer.hobby_list = "basket, movies"


ACT_AS_TAGGABLE_ON | CONSULTAS
# Productos con un tag en particular
@product.tagged_with("sport")
# Productos asociados a todos los tags indicados
@product.tagged_with(["sport", "black"], match_all: true)
# Productos con al menos uno de los tags indicados
@product.tagged_with(["shoes", "black"], any: true)
# Productos no vinculados a los tags indicados
@product.tagged_with("black", exclude: true)
# Productos con tags similares (%valor%) al valor indicado
@product.tagged_with("shoe", wild: true) # %shoe%
# Lista de tags vinculados al producto
@product.tag_list
# Otros Productos vinculados a los mismos tags
@product.find_related_tags
AGENDA
ATTACHMENTS
TAGS
REFERENCIAS
https://www.railstutorial.org
http://guides.rubyonrails.org

You might also like