cirandas.net

ref: master

db/migrate/20160422163123_enable_products_plugin_on_environments.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Product < ApplicationRecord
end
class Profile < ApplicationRecord
  has_many :products
end
class Environment < ApplicationRecord
  has_many :profiles
  has_many :products, through: :profiles, class_name: '::Product'

  extend ActsAsHavingSettings::ClassMethods
  acts_as_having_settings field: :settings
  settings_items :enabled_plugins, type: Array
end

class EnableProductsPluginOnEnvironments < ActiveRecord::Migration

  def up
    environments  = Environment.all
    products_used = environments.any?{ |e| e.products.count > 0 }
    return unless products_used

    Bundler.clean_system 'script/noosfero-plugins enable products'
    environments.each do |e|
      products = e.products.where('profiles.visible = true')
      next unless products.count > 0
      e.enabled_plugins << 'ProductsPlugin'
      e.save!
    end
  end

  def down
    say "this migration can't be reverted"
  end

end