RubyChina如何实现喜欢功能?
2012-08-31 11:16
323 查看
RubyChina有一个喜欢功能,具体的表现可以查看每一个帖子的页面
如:
http://ruby-china.org/topics/5272
实现的代码解析如下:
在topics/show.html.erb页面中, 有如下代码
likeable_tag 是定义在/app/helpers/likes_helper.rb中的helper方法
具体代码如下
此处在于构造一个link_to tag,并且bind了一个click方法
App.likeable的方法定义在
具体实现如下
看看服务器端的action吧。
定义了两个action: create 和 destroy , 分别对应 喜欢 和 取消喜欢 , 对来自与客户端的 type 参数,做了过滤
在models/topic.rb中,include的一个module
likeable的定义
定义了 两个字段
liked_user_ids 和 likes_count
并定义了一个方法
controller中使用了 user.like 方法
以上功能的实现,使用了 Ruby的 module 引入和 duck type, 有很多值得学习的地方。
如:
http://ruby-china.org/topics/5272
实现的代码解析如下:
在topics/show.html.erb页面中, 有如下代码
<%= likeable_tag(@topic) %>
likeable_tag 是定义在/app/helpers/likes_helper.rb中的helper方法
具体代码如下
def likeable_tag(likeable) return "" if likeable.blank? label = "#{likeable.likes_count}人喜欢" if likeable.likes_count == 0 label = "喜欢" end if current_user && likeable.liked_by_user?(current_user) title = "取消喜欢" state = "liked" icon = content_tag("i", "", :class => "icon small_liked") else title = "喜欢" state = "" icon = content_tag("i", "", :class => "icon small_like") end like_label = raw "#{icon} <span>#{label}</span>" link_to(like_label,"#",:title => title, :rel => "twipsy", 'data-count' => likeable.likes_count, 'data-state' => state,'data-type' => likeable.class,'data-id' => likeable.id, :class => 'likeable', :onclick => "return App.likeable(this);") end
此处在于构造一个link_to tag,并且bind了一个click方法
return App.likeable(this);
App.likeable的方法定义在
app/assets/javascripts/app.coffee中
具体实现如下
likeable : (el) -> $el = $(el) likeable_type = $el.data("type") likeable_id = $el.data("id") likes_count = parseInt($el.data("count")) if $el.data("state") != "liked" $.ajax url : "/likes" type : "POST" data : type : likeable_type id : likeable_id likes_count += 1 $el.data("state","liked").data('count', likes_count).attr("title", "取消喜欢") $('span',el).text("#{likes_count}人喜欢") $("i.icon",el).attr("class","icon small_liked") else $.ajax url : "/likes/#{likeable_id}" type : "DELETE" data : type : likeable_type if likes_count > 0 likes_count -= 1 $el.data("state","").data('count', likes_count).attr("title", "喜欢") if likes_count == 0 $('span',el).text("喜欢") else $('span',el).text("#{likes_count}人喜欢") $("i.icon",el).attr("class","icon small_like") false
看看服务器端的action吧。
# coding: utf-8 class LikesController < ApplicationController before_filter :require_user before_filter :find_likeable def create current_user.like(@item) render :text => @item.reload.likes_count end def destroy current_user.unlike(@item) render :text => @item.reload.likes_count end private def find_likeable @success = false @element_id = "likeable_#{params[:type]}_#{params[:id]}" if not params[:type].in?(['Topic','Reply']) render :text => "-1" return false end klass = params[:type].constantize @item = klass.find_by_id(params[:id]) if @item.blank? render :text => "-2" return false end end end
定义了两个action: create 和 destroy , 分别对应 喜欢 和 取消喜欢 , 对来自与客户端的 type 参数,做了过滤
if not params[:type].in?(['Topic','Reply'])并将字符串 转换成 类
klass = params[:type].constantize
在models/topic.rb中,include的一个module
include Mongoid::Likeable
likeable的定义
# coding: utf-8 module Mongoid module Likeable extend ActiveSupport::Concern included do field :liked_user_ids, :type => Array, :default => [] field :likes_count, :type => Integer, :default => 0 end defliked_by_user?(user) return false if user.blank? self.liked_user_ids.include?(user.id) end end end
定义了 两个字段
liked_user_ids 和 likes_count
并定义了一个方法
liked_by_user?
controller中使用了 user.like 方法
# 收藏东西 def like(likeable) return false if likeable.blank? return false if likeable.liked_by_user?(self) likeable.push(:liked_user_ids, self.id) likeable.inc(:likes_count, 1) endlike方法就是想数组中push 数据
以上功能的实现,使用了 Ruby的 module 引入和 duck type, 有很多值得学习的地方。
相关文章推荐
- RubyChina如何实现软删除?
- 如何实现“猜你喜欢”功能?
- iOS 用其他应用程序打开文件功能如何实现的iOS开发 - CocoaChina CocoaChina_让移动开发更简单
- 如何实现“猜你喜欢”功能
- 如何实现用户注册时记住账号和密码的功能?
- Ruby on Rails实现最基本的用户注册和登录功能的教程
- 如何利用Skyline6.1实现多球对比功能
- 看WIZ110SR如何实现串口转以太网功能
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- android 如何使用spinner来实现选择省份和市区功能
- C语言如何实现遍历目录的功能
- EasyNVR无插件直播服务器如何使用ffmpeg实现摄像机快照功能的
- android如何实现注销功能
- 文件数量较多的情况下如何提高刻录速度(调用IMAPI2实现DVD刻录功能)
- 如何实现一个类似与电子口令卡的密码验证的功能
- 如何有效实现应用系统的增删改查功能
- 举例说明android如何实现发送短信的功能
- 想问一下CU博客的二级域名功能如何实现
- 如何用润乾报表实现进度条功能
- Linux下如何实现U盘、SD卡自动挂载功能