为社交化Ruby on Rails创建视图

时间:2014-10-02 15:49:37

标签: ruby-on-rails ruby ruby-on-rails-4

社会化宝石似乎拥有我可能需要或想要的所有模型/结构。但是,没有其他任何东西可以帮助您完成项目。由于文档很少,我的问题是独一无二的。我必须转向stackoverflow。

首先,让我先谈谈我使用这个宝石的方向。我想使用public_activity gem的跟踪功能创建包含社交Feed的主题区域。在仪表板上,用户将看到他们最近的活动和过去的评论。我想要做的是弄清楚我应该如何将系统结构化为DRY并且易于自定义/添加功能。

当前项目包括

posts_controller.rb

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json

  before_filter :authenticate_user!, only: [:create, :new, :edit, :destroy]
  def dashboard
    @posts = current_user.posts

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @comment = Comment.new
    @post = Post.find(params[:id])
    @comments = @post.comments

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])
    @post.user = current_user

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

socializations_controller.rb

class SocializationsController < ApplicationController
  before_filter :load_socializable

  def follow
    current_user.follow!(@socializable)
    render json: {follow: true}
  end

  def unfollow
    current_user.unfollow! (@socializable)
    render json: { follow: false}
  end

  private
  def load_socializable
    @socializable =
        case
          when id = params[:comment_id] # Must be before :item_id, since it's nested under it.
            @community.comments.find(id)
          when id = params[:item_id]
            @community.items.find(id)
          else
            raise ArgumentError, "Unsupported socializable model, params: " +
                params.keys.inspect
        end
    raise ActiveRecord::RecordNotFound unless @socializable
  end

end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  has_attached_file :avatar, :styles => { :medium => "300x300", :thumb => "100x100"}, :default_url => "/images/:style/missing.png"
                    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

  validates :username, presence: true, length: {maximum: 255}, uniqueness: {case_sensitive: false }, format: {with: /\A[a-zA-Z0-9]*\z/, message: "may only contain letters and numbers." }
  validates :avatar, :attachment_presence => true
  validates_with AttachmentPresenceValidator, :attributes => :avatar
  validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 3.megabytes
  validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/]

  # Prepare mailboxer for user
  acts_as_messageable


  devise :database_authenticatable,
         :registerable,
         :recoverable,
         :rememberable,
         :trackable,
         :validatable

  #Mailer Methods
  def name
    email
  end

  def mailboxer_email(object)
    email
  end

  #Methods for Socialization User attributes
  acts_as_follower
  acts_as_followable
  has_many :posts
  has_many :comments
  acts_as_liker
  acts_as_likeable
  acts_as_mentionable


  attr_accessor :login

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first
    else
      where(conditions).first
    end
  end

end

post.rb

class Post < ActiveRecord::Base
  attr_accessible :content, :user_id, :avatar
  belongs_to :user
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  has_many :comments
  include PublicActivity::Model
  tracked owner: ->(controller, model) { controller && controller.current_user }
  acts_as_likeable
end

可用的视图

帖子

_form.html.erb

<%= form_for @post, html: { multipart: true } do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.text_area :content %>
  </div>
  <%= f.file_field :avatar %>
  <div class="actions">
    <%= f.submit class: 'small button' %>
  </div>
<% end %>

_post.html.erb

<div class="user-box">
<%= image_tag post.user.avatar.url(:thumb), class: 'profile_post' %>
<%= link_to post.user.name, post.user %>
</div>
  <%= post.content %>
</p>
<%= image_tag post.avatar.url if post.avatar.exists? %>
    <div class="<%= post.class %>-<%= post.id %>">
<%= render 'shared/like_form', likeable: post %>
<%= link_to pluralize(post.comments.count, 'comment'), post, class: 'comments' %>
<div class="likes"><%= pluralize(post.likers(User).count, 'like')%></div>
</div>

帖子\ edit.html.erb

<h1>Editing post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

帖子\ new.html.erb

<h1>New post</h1>

<%= render 'form' %>

<%= link_to 'Back', posts_path %>

帖子\ show.html.erb

<p id="notice"><%= notice %></p>
<div class="user-box">
<%= image_tag @post.user.avatar.url(:thumb), class: 'profile' %>
<%= link_to @post.user.name, @post.user %>
</div>
<p>
  <%= @post.content %>
</p>
<%= image_tag @post.avatar.url if @post.avatar.exists? %>
    <div class="<%= @post.class %>-<%= @post.id %>">
<%= render 'shared/like_form', likeable: @post %>
<%= link_to pluralize(@comments.count, 'comment'), @post, class: 'comments' %>
<div class="likes"><%= pluralize(@post.likers(User).count, 'like')%></div>
</div>
<%= render 'shared/form' %>
<%= render @comments  %>

的routes.rb

resources :users
  resources :posts
  resources :comments

0 个答案:

没有答案