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,分别表示节点数.初始边数.操作数 ...
随机推荐
- cin
cin 是预定义的标准输入流对象,cin 用来接收字符串时,遇“空格”.“TAP”.“回车”时都会结束.
- event.srcElement ,event.fromElement,event.toElement
自然,我们都习惯了 IE,在 IE 中要在函数中获得各事件对象很容易,直接用 event.event.srcElemtn.event.fromElement.event.toElement 就行了.在 ...
- 关于DOS与cmd(windows系统)
dos是计算机的最初期的操作系统,对电脑操作必须输入各种dos命令窗口,可以理解成运行计算机机器内部语言,知道编程吗?其实早期dos命令操作系统就是运行计算机内部的编程命令,因此操作人员都必须具有一定 ...
- 浅谈对Spring Framework的认识
Spring Framework,作为一个应用框架,官方的介绍如下: The Spring Framework provides a comprehensive programming and con ...
- 支持断线重连、永久watcher、递归操作并且能跨平台(.NET Core)的ZooKeeper异步客户端
在公司内部的微服务架构中有使用到了"ZooKeeper",虽然官方有提供了.NET的SDK,但易用性非常的差,且搜遍github.nuget,没有发现一个可以跨平台且易用的组件,所 ...
- CSS基本知识1-CSS基本概念
CSS基本概念: 选择器{属性:值;属性:值} CSS继承:子元素继承父元素样式,父子关系看DOM结构. CSS覆盖: 浏览器缺省设置 外部样式表 内部样式表(位于 <head> 标签内部 ...
- CSS编写技巧
1.尽量少的使用全局的重置代码 全局重置代码:*{margin:0; padding:0;}对于熟悉CSS的人来说并不陌生,并且有很多人的第一句CSS代码就是这句.它可以避免不同浏览器的默认间距不同而 ...
- Activity数据传递
1.在启动界面里通过intent调用方法putExtra添加欲携带数据 2.在被启动界面里通过getIntent方法获取Intent对象 3.通过intent的getXxxExtra方法获取对应的数据
- socket编程知识
Socket语法及相关: Socket Familile(地址簇) socket.AF_UNIX unix本机进程间通信 socket.AF_INET IPV4 经常用的还是这里 so ...
- jQuery插件 -- Cookie插件jquery.cookie.js(转)
Cookie是网站设计者放置在客户端的小文本文件.Cookie能为用户提供很多的使得,例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存 ...