首页 > 代码库 > sujection重构
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
修改后
只有在新建一个 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 endrescue ArgumentError, Youku::DS::Error => e @subjection.errors.add(:base, e.message) render :errorendprivate 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 endend
sujection重构
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。