adds a new user settings system + migration + rake task/worker updates

This commit is contained in:
Manuel Wiedenmann 2013-06-06 03:40:15 +02:00
parent e8173d44ae
commit 60c4c5510a
11 changed files with 141 additions and 68 deletions

View file

@ -0,0 +1,17 @@
class CreateSettings < ActiveRecord::Migration
def self.up
create_table :settings do |t|
t.string :var, null: false
t.text :value, null: true
t.integer :thing_id, null: true
t.string :thing_type, limit: 30, null: true
t.timestamps
end
add_index :settings, [ :thing_type, :thing_id, :var ], unique: true
end
def self.down
drop_table :settings
end
end

View file

@ -0,0 +1,34 @@
class MigrateUserSettings < ActiveRecord::Migration
def up
old_settings = ConfigurableSetting.all
old_settings.each do |old_setting|
# get target (user)
type = old_setting.configurable_type
id = old_setting.configurable_id
user = type.constantize.find(id)
# get the data (settings)
name = old_setting.name
namespace = name.split('.')[0]
key = name.split('.')[1].underscore # Camelcase to underscore
# prepare value
value = YAML.load(old_setting.value)
value = value.nil? ? false : value
# set the settings_attributes (thanks to settings.merge! we can set them one by one)
user.settings_attributes = {
"#{namespace}" => {
"#{key}" => value
}
}
# save the user to apply after_save callback
user.save
end
end
def down
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121230142516) do
ActiveRecord::Schema.define(:version => 20130605235825) do
create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false
@ -268,6 +268,17 @@ ActiveRecord::Schema.define(:version => 20121230142516) do
add_index "pages", ["permalink"], :name => "index_pages_on_permalink"
add_index "pages", ["title"], :name => "index_pages_on_title"
create_table "settings", :force => true do |t|
t.string "var", :null => false
t.text "value"
t.integer "thing_id"
t.string "thing_type", :limit => 30
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "settings", ["thing_type", "thing_id", "var"], :name => "index_settings_on_thing_type_and_thing_id_and_var", :unique => true
create_table "stock_changes", :force => true do |t|
t.integer "delivery_id"
t.integer "order_id"