王筝的博客
ruby学习

 

 

 

  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

 

need_fetch_channel.each do |channel|
    yesterday = (Time.now - 86000).to_i
    all_channel_videos = channel.videos.asc(:begin_time)
    all_channel_videos.each do |v| 
      if v.end_time < yesterday
        v.destroy
      end 
    end 
end

重构完

need_fetch_channel.videos.where(:end_time.lt => DateTime.yesterday.midnight).destroy_all

DateTime.yesterday.midnight 获取的是昨天的凌晨零点的时间

目前我们项目中的 CSS/JS 文件比较多, 由于RAILS 3.0 没有提供asset pipeline功能,所以这样会制约我们的访问速度。
例如:  目前,我们的布局( origin.html.erb )页面有 19 个JS文件,15个CSS文件。 每次打开都需要发送 34个 request,严重影响体验。
所以,我们要把这些js, css 分别打包压缩成一个文件。

参考: http://stackoverflow.com/questions/7112218/how-to-install-a-plugin-in-rails-3-getting-a-commands-is-not-a-module-typeerro/23507780#23507780

1. 编辑  Rakefile: 
  require File.expand_path('../config/application', __FILE__)
  require 'rake'
  # 增加这一行,注意位置。
  include Rake::DSL
2.编辑: script/rails:
  APP_PATH = File.expand_path('../../config/application',  __FILE__)
  require File.expand_path('../../config/boot',  __FILE__)
  # 增加这一行,注意位置。
  module Commands; end
  require 'rails/commands'

3.then run this command:

  $ bundle exec rails plugin install git://github.com/sbecker/asset_packager.git
4. 编辑: 新增一个 yml 文件, 把你用到的JS, CSS文件放到里面去: $ cat config/asset_packages.yml
---
javascripts:
- base:
  - jquery-1.9.1.min
  - bootstrap.min
  - jquery-migrate-1.1.1
  - jquery-ui-1.10.1.custom.min
  - jquery-ujs-for-jquery-1.9
  - jquery.treeview
  - jquery.toastmessage
  - jquery-autocomplete-combobox
  - jquery.uploadify
  - jquery-ui-timepicker-addon
  - jquery.ui.datepicker-zh-CN
  - select2
  - select2_locale_zh-CN
  - global
  - jquery.tagit
  - jquery.validate
  - jqueryui-editable
  - jquery.ui.widget

stylesheets:
- base:
  - style
  - invalid
  - reset
  - jquery.treeview
  - jquery-ui-1.10.1.custom
  - jquery.toastmessage
  - jquery-autocomplete-combobox
  - uploadify
  - select2
  - jquery.tagit
  - jquery.validate
  - cms
  - jqueryui-editable
  - bootstrap.min
  - customized_bootstrap
5. 在 布局文件(origin.html.erb)中:
  <%= raw stylesheet_link_merged :base %>
  <%= raw javascript_include_merged :base %>
6. 编辑 .gitignore,增加这两行(忽略掉他们)
public/javascripts/base_packaged.js
public/stylesheets/base_packaged.css
7. 最新发现: 压缩后的 js 会在FIREFOX下面工作不正常。为了稳妥,我们只使用合并后的JS, 而不对其压缩:
  # vim vendor/plugins/asset_packager/lib/synthesis/asset_package.rb
128       def create_new_build
              ......
              # 记得要修改这行代码。 ( 使用merged_file ,而不是compressed_file ) 
133           #File.open(new_build_path, "w") {|f| f.write(compressed_file) }
134           File.open(new_build_path, "w") {|f| f.write( merged_file ) }
              ......
137       end

就可以了。

更多,见: https://github.com/sbecker/asset_packager

8. 关于调试时出现的问题:

如果发现某个JS 或者 CSS 文件是空白,  那么就删掉它们, 刷新页面。
$ rm public/javascripts/base_packaged.js public/stylesheets/base_packaged.css

刷新页面之后,就会看到新的 js, css 文件都已经生成了。

由于edit和new页面的相似部分很多,需要提取出来,现在就是提取的方法

从form 的开始部分选中,shift+v选中对应的行

:Rextract form

然后别忘记:wa

这样就会生成一个_form文件