zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    测试我的第一个随笔
    ORB-SLAM2初步
    STL map 简介
    day4-内置函数
    Python学习之路day4-函数高级特性、装饰器
    Python学习之路day3-函数
    Python学习之路day3-字符编码与转码
    Python学习之路day3-文件操作
    Python学习之路day3-集合
    Python学习之路【第二篇】-pyc简介、Python常用的数据类型及其用法和常用运算符
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/5889958.html
Copyright © 2011-2022 走看看