Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #models/item.rb
- class Item < ActiveRecord::Base
- attr_accessible :item_votes_count, :name, :price, :taobaoke_url, :original_url, :topic_id, :image, :image_cache, :remote_image_url
- belongs_to :topic, counter_cache: true
- belongs_to :user
- has_many :item_votes, dependent: :destroy
- validates :name, :taobaoke_url, :topic_id, :original_url, presence: true
- validates :price, presence: true, numericality: true
- mount_uploader :image, ItemImageUploader
- store_in_background :image
- scope :order_by_votes, order('item_votes_count desc')
- scope :order_by_create_time, order('created_at desc')
- def vote(by)
- return false unless by
- return false if voted_by?(by)
- item_votes.create!(vote: 1, user_id: by.id)
- end
- def voted_by?(user)
- return false unless user
- !item_votes.by_user(user).size.zero?
- end
- def votes(options = {})
- reload = options[:reload]
- reload ? item_votes.count : item_votes.size
- end
- end
- #uploaders/item_image_uploader.rb
- class ItemImageUploader < CarrierWave::Uploader::Base
- include CarrierWave::MiniMagick
- include CarrierWave::Backgrounder::Delay
- def store_dir
- "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
- end
- # Create different versions of your uploaded files:
- version :s250x300 do
- process resize_to_fill: [250, 300]
- end
- version :s100 do
- process resize_to_fill: [100, 100]
- end
- end
- #models/topic.rb
- class Topic < ActiveRecord::Base
- attr_accessible :desc, :items_count, :name, :button_name, :user_id, :image, :image_cache
- has_many :items, dependent: :destroy
- belongs_to :user, counter_cache: true
- validates :name, :button_name, :user_id, presence: true
- mount_uploader :image, TopicImageUploader
- store_in_background :image
- end
- #controllers/topics_controller.rb
- class TopicsController < ApplicationController
- load_and_authorize_resource
- before_filter :require_login, except: [:index, :show]
- def index
- count = params[:count] ? params[:count].to_i : 12
- page = params[:page] ? params[:page].to_i : 1
- @topics = Topic.page(page).per(count)
- respond_to do |format|
- format.html # index.html.erb
- format.json { render json: @topics }
- end
- end
- #views/topics/index.html.erb
- <header class="header">
- <a href="/"><span class="logo"></span></a>
- <%= link_to('New Topic', new_topic_path) if can? :create, Topic %>
- </header>
- <section class="wrapper">
- <ul class="topic-list">
- <% @topics.each do |topic| %>
- <li>
- <a href="<%= topic_path(topic) %>">
- <h3 class="topic-list-title"><%= topic.name %></h3>
- <div class="topic-list-imgs">
- <% items = topic.items.order_by_votes.first(9) %>
- <% if !items.empty? %>
- <ul>
- <% items.each do |item| %>
- <li>
- <img src="<%= item.image_url(:s100) %>" alt="<%= topic.name %>">
- </li>
- <% end %>
- </ul>
- <% else %>
- <img src="<%= topic.image_url(:s300) %>" alt="<%= topic.name %>">
- <% end %>
- </div>
- </a>
- </li>
- <% end -%>
- </ul>
- <%= paginate @topics %>
- </section>
Advertisement
Add Comment
Please, Sign In to add comment