SHOW:
|
|
- or go back to the newest paste.
| 1 | #models/item.rb | |
| 2 | class Item < ActiveRecord::Base | |
| 3 | attr_accessible :item_votes_count, :name, :price, :taobaoke_url, :original_url, :topic_id, :image, :image_cache, :remote_image_url | |
| 4 | belongs_to :topic, counter_cache: true | |
| 5 | belongs_to :user | |
| 6 | has_many :item_votes, dependent: :destroy | |
| 7 | ||
| 8 | validates :name, :taobaoke_url, :topic_id, :original_url, presence: true | |
| 9 | validates :price, presence: true, numericality: true | |
| 10 | ||
| 11 | mount_uploader :image, ItemImageUploader | |
| 12 | store_in_background :image | |
| 13 | ||
| 14 | scope :order_by_votes, order('item_votes_count desc')
| |
| 15 | scope :order_by_create_time, order('created_at desc')
| |
| 16 | ||
| 17 | def vote(by) | |
| 18 | return false unless by | |
| 19 | return false if voted_by?(by) | |
| 20 | item_votes.create!(vote: 1, user_id: by.id) | |
| 21 | end | |
| 22 | ||
| 23 | def voted_by?(user) | |
| 24 | return false unless user | |
| 25 | !item_votes.by_user(user).size.zero? | |
| 26 | end | |
| 27 | ||
| 28 | def votes(options = {})
| |
| 29 | reload = options[:reload] | |
| 30 | reload ? item_votes.count : item_votes.size | |
| 31 | end | |
| 32 | ||
| 33 | end | |
| 34 | ||
| 35 | #uploaders/item_image_uploader.rb | |
| 36 | class ItemImageUploader < CarrierWave::Uploader::Base | |
| 37 | include CarrierWave::MiniMagick | |
| 38 | include CarrierWave::Backgrounder::Delay | |
| 39 | ||
| 40 | def store_dir | |
| 41 | "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
| |
| 42 | end | |
| 43 | # Create different versions of your uploaded files: | |
| 44 | version :s250x300 do | |
| 45 | process resize_to_fill: [250, 300] | |
| 46 | end | |
| 47 | ||
| 48 | version :s100 do | |
| 49 | process resize_to_fill: [100, 100] | |
| 50 | end | |
| 51 | end | |
| 52 | ||
| 53 | ||
| 54 | #models/topic.rb | |
| 55 | class Topic < ActiveRecord::Base | |
| 56 | attr_accessible :desc, :items_count, :name, :button_name, :user_id, :image, :image_cache | |
| 57 | has_many :items, dependent: :destroy | |
| 58 | belongs_to :user, counter_cache: true | |
| 59 | ||
| 60 | validates :name, :button_name, :user_id, presence: true | |
| 61 | ||
| 62 | mount_uploader :image, TopicImageUploader | |
| 63 | store_in_background :image | |
| 64 | ||
| 65 | end | |
| 66 | ||
| 67 | ||
| 68 | #controllers/topics_controller.rb | |
| 69 | class TopicsController < ApplicationController | |
| 70 | load_and_authorize_resource | |
| 71 | before_filter :require_login, except: [:index, :show] | |
| 72 | ||
| 73 | def index | |
| 74 | count = params[:count] ? params[:count].to_i : 12 | |
| 75 | page = params[:page] ? params[:page].to_i : 1 | |
| 76 | @topics = Topic.page(page).per(count) | |
| 77 | ||
| 78 | respond_to do |format| | |
| 79 | format.html # index.html.erb | |
| 80 | format.json { render json: @topics }
| |
| 81 | end | |
| 82 | end | |
| 83 | ||
| 84 | ||
| 85 | #views/topics/index.html.erb | |
| 86 | <header class="header"> | |
| 87 | <a href="/"><span class="logo"></span></a> | |
| 88 | <%= link_to('New Topic', new_topic_path) if can? :create, Topic %>
| |
| 89 | </header> | |
| 90 | ||
| 91 | <section class="wrapper"> | |
| 92 | <ul class="topic-list"> | |
| 93 | <% @topics.each do |topic| %> | |
| 94 | <li> | |
| 95 | <a href="<%= topic_path(topic) %>"> | |
| 96 | <h3 class="topic-list-title"><%= topic.name %></h3> | |
| 97 | <div class="topic-list-imgs"> | |
| 98 | <% items = topic.items.order_by_votes.first(9) %> | |
| 99 | <% if !items.empty? %> | |
| 100 | <ul> | |
| 101 | <% items.each do |item| %> | |
| 102 | <li> | |
| 103 | <img src="<%= item.image_url(:s100) %>" alt="<%= topic.name %>"> | |
| 104 | </li> | |
| 105 | <% end %> | |
| 106 | </ul> | |
| 107 | <% else %> | |
| 108 | <img src="<%= topic.image_url(:s300) %>" alt="<%= topic.name %>"> | |
| 109 | <% end %> | |
| 110 | </div> | |
| 111 | </a> | |
| 112 | </li> | |
| 113 | <% end -%> | |
| 114 | </ul> | |
| 115 | <%= paginate @topics %> | |
| 116 | </section> |