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,分别表示节点数.初始边数.操作数 ...
随机推荐
- JavaScript 输出
javascript 没有任何打印或者输出的函数. 一.javascript 显示数据 javacript可以通过不同的方式来输出数据: 1.使用window.alert()弹出警告框 2.使用doc ...
- AngularJS之开发组件的一些思路
欢迎大家指导与讨论 : ) 一 .前言 由于笔者水平有限,在这里只是分享自己开发组件的一些思路~ 摘要:无UI组件类.有UI组件类.有UI组件类型2.欢迎拍砖吐槽 O(∩_∩)O 二 .无UI组件类 ...
- 搭建一套自己实用的.net架构(1)【概述】
入园很久,一直默默的潜水,近来得空想写点什么. 思前想后,那就把自己平时没事干自己摘抄.引用.瞎写的一些东西写出来.帮助自己巩固一下,顺便请高手们指点一二. 我本人很懒 ,一些代码就是直接复制别人的劳 ...
- 【跟着子迟品 underscore】Object Functions 相关源码拾遗 & 小结
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- 【转】C++之父:C++ 的五个普遍误解
文章三部分如下: 1.http://blog.jobbole.com/82460/ 2.http://blog.jobbole.com/82461/ 3.http://blog.jobbole.com ...
- Charles 从入门到精通
特别说明:原文来自唐巧大神的博客 http://blog.devtang.com/2015/11/14/charles-introduction/ Charles 从入门到精通 文章目录 1. 目录及 ...
- java中Arraylist复制方法
方法一: ArrayList<Integer> mycopy=new ArrayList<Integer>(); mycopy=(ArrayList<Integer> ...
- [CG编程] 基本光照模型的实现与拓展以及常见光照模型解析
0.前言 这篇文章写于去年的暑假.大二的假期时间多,小组便开发一个手机游戏的项目,开发过程中忙里偷闲地了解了Unity的shader编写,而CG又与shaderLab相似,所以又阅读了<CG教程 ...
- 精通Web Analytics 2.0 (7) 第五章:荣耀之钥:度量成功
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第五章:荣耀之钥:度量成功 我们的分析师常常得不到我们应得的喜欢,尊重和资金,因为我们没有充分地衡量一个黄金概念:成果.因为我们 ...
- Leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...