sujection重构
def create
@subjection = @subject.subjections.new
if params[:video_or_show_id].length == 20
show = Show.where(:showid => params[:video_or_show_id]).try(:first)
if show.present?
@show = show
else
begin
@show = Show.build_from_show_id(params[:video_or_show_id])
@subjection.subjectable_type = "show"
@subjection.subjectable_id = @show.id
@subjection.title = @show.showname
@subjection.sub_title = @show.showsubtitle
@subjection.description = @show.showdesc
@subjection.save
if @show.save
img_v = @subjection.pictures.new(:url => @show.show_vthumburl,
:name => "img_v")
img_v.save
img_h = @subjection.pictures.new(:url => @show.show_thumburl,
:name => "img_h")
img_h.save
end
rescue ArgumentError, Youku::DS::Error => e
@show = Show.new
@show.errors.add(:base, e.message)
redirect_to new_subject_subjection_path(@subject), :notice => "视频没有版权"
end
end
else
video = Video.where(:videoid => params[:video_or_show_id]).try(:first)
if video.present?
@video = video
else
begin
@video = Video.build_from_video_id(params[:video_or_show_id])
if @video.videoid.present?
@subjection.subjectable_type = "video"
@subjection.subjectable_id = @video.id
@subjection.description = @video.desc
@subjection.title = @video.title
@subjection.save
if @video.save
img_h = @subjection.pictures.new(:url => @video.thumburl,
:name => "img_h")
img_h.save
end
end
rescue ArgumentError, Youku::DS::Error => e
@video = Video.new
@subjection.errors.add(:base, e.message)
render :error and return
end
end
end if @subjection.save
redirect_to subject_subjections_url(@subject), notice: '新建成功'
else
render :new
end
end
修改后
if persisted? # 如果是已经保存到数据库的 ActiveRecord 对象
super # 则调用原始的 subjectable= 方法
只有在新建一个 subjection 时,调用 subjectable= 方法的时做一些特殊处理,
比如给 title, sub_title 和 description 赋值
def create
@subjection = @subject.subjections.build
id = params[:video_or_show_id] subjectable = if id.length == 20
find_or_create_show_from_show_id(id)
else
find_or_create_video_from_video_id(id)
end @subjection.subjectable = subjectable if @subjection.save
redirect_to subject_subjections_url(@subject), notice: '新建成功'
else
render :error
end
rescue ArgumentError, Youku::DS::Error => e
@subjection.errors.add(:base, e.message)
render :error
end private
def find_or_create_show_from_show_id(id)
if show = Show.find_by_showid(id)
show
else
show = Show.build_from_show_id(id)
show.save
show
end
end def find_or_create_video_from_video_id(id)
if video = Video.find_by_videoid(id)
video
else
video = Video.build_from_video_id(id)
video.save
video
end
end
def subjectable=(subjectable)
if persisted?
super
else
self.attributes = {
subjectable_type: subjectable.class.name,
subjectable_id: subjectable.id
} case subjectable
when Show
self.attributes = {
title: subjectable.showname,
sub_title: subjectable.showsubtitle,
description: subjectable.showdesc,
pictures_attributes: [{ name: 'img_h', url: subjectable.show_thumburl },
{ name: 'img_v', url: subjectable.show_vthumburl }]
}
when Video
self.attributes = {
title: subjectable.title,
description: subjectable.desc,
pictures_attributes: [{ name: 'img_h', url: subjectable.thumburl }]
}
else
super
end
end
end
sujection重构的更多相关文章
- 记一次.NET代码重构
好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使劲赶的时候,神马设计都是浮云 ...
- CSharpGL(17)重构CSharpGL
CSharpGL(17)重构CSharpGL CSharpGL用起来我自己都觉得繁琐了,这是到了重构的时候. 下载 CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入(https ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(58)-DAL层重构
系列目录 前言:这是对本文系统一次重要的革新,很久就想要重构数据访问层了,数据访问层重复代码太多.主要集中增删该查每个模块都有,所以本次是为封装相同接口方法 如果你想了解怎么重构普通的接口DAL层请查 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(59)-BLL层重构
系列目录 前言: 这应该是本系统最后一次重构,将重构BLL层和Model层.来完全取代代码生成器生成的BLL层和DAL层.完全废掉了代码生成器的DAL,BLL,MODEL层. 全自动生成增,删,改 ...
- 原生JS实现全屏切换以及导航栏滑动隐藏及显示——重构前
思路分析: 向后滚动鼠标滚轮,页面向下全屏切换:向前滚动滚轮,页面向上全屏切换.切换过程为动画效果. 第一屏时,导航栏固定在页面顶部,切换到第二屏时,导航条向左滑动隐藏.切换回第一屏时,导航栏向右滑动 ...
- Android重构与设计之路,从整理提示弹窗(SmartAlertPop)开始
封装一个独立弹窗Module,这里的弹窗包括普通的Dialog方式弹框和WindowManager方式弹窗.提供一种管理项目里面弹窗的方案,便于后期修改和维护. 首先描述一个在大项目中普遍存在的一个现 ...
- 产品前端重构(TypeScript、MVC框架设计)
最近两周完成了对公司某一产品的前端重构,本文记录重构的主要思路及相关的设计内容. 公司期望把某一管理类信息系统从项目代码中抽取.重构为一个可复用的产品.该系统的前端是基于 ExtJs 5 进行构造的, ...
- 一枚招聘信息——分期乐招页面重构/UI开发(8k-12k 深圳 经验1-3年 学历不限 全职)
腾讯人创建的分期乐公司,急需页面重构人才若干枚,公司前景好,机会难得,有兴趣的速速 8k-12k 深圳 经验1-3年 学历不限 全职 公司网站: http://www.fenqile.com/ 职位诱 ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
随机推荐
- Hibernate考试选择题解析
1.在Hibernate中,以下关于主键生成器说法错误的是(AC). A.increment可以用于类型为long.short或byte的主键(byte类型不可以) B.identity用于如SQL ...
- Hibernate选择题总结
Hibernate选择题总结 1.在Hibernate中,以下关于主键生成器说法错误的是( ). A.increment可以用于类型为long.short或byte的主键 B.identity用于如S ...
- 面向移动设备的html5开发框架
很久以前整理了篇将手机网站做成手机应用的JS框架.时隔一年多,很多新的技术已经出现,下面再来总结下还有哪些框架是适合面向手机设备的开发的. 1.jQuery Mobile jQuery Mobile ...
- Unity3D 预设打包的注意事项
在平时的开发中,把预设打包成 assetbundle 文件是非常普遍的做法,但是我们不能随便把预设打包成 assetbundle 就算完事,我们应该先清楚把预设打包成 assetbundle 的目的, ...
- oracle如何清空一个用户下的所有表中的数据?
-- 大概 这个样子,如果没有 FK 的话,一下子就都删掉了. begin for x in (select table_name from user_tables) loop execute imm ...
- Dump中查看dictionary信息的方法
In order to dump the contents of a dictionary, you need to start with either the MethodTable or the ...
- JS组件系列——不容错过的两款Bootstrap Icon图标选择组件
前言:最近好多朋友在群里面聊到bootstrap icon图标的问题,比如最常见的菜单管理,每个菜单肯定需要一个对应的菜单图标,要是有一个可视化的图标选择组件就好了,最好是直接选择图标,就能得到对应的 ...
- 由Memcached升级到 Couchbase的 Java 客户端的过程记录(二)
Shiro提供了类似于Spring的Cache抽象,即Shiro本身不实现Cache,但是对Cache进行了又抽象,方便更换不同的底层Cache实现. shiro对缓存的支持 shiro并没有实现缓存 ...
- ID还是普通字段做外键合适?
ORACLE:USER表中没有ID字段,只有USERNAME做为一个唯一一字段当主键, COMMENT评论表中有一个user表的外键是用了USER表中的USERNAME字段. 我总认为这不合理,一般情 ...
- CentOS Linux解决Device eth0 does not seem to be present
在VMware里克隆出来的Centos Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: 解决办法: 首先,打开/etc/udev/rules.d/70 ...