diff --git a/README.md b/README.md index 4743f20..baaed2e 100644 --- a/README.md +++ b/README.md @@ -93,13 +93,13 @@ end # :manufacturer and :year should be Car's instance methods(like ActiveRecord columns) class Car < ActiveRecord::Base - protokoll :code, :scoped_by => lambda { |o| "#{o.manufacturer}-#{o.year}" } + protokoll :code, :scope_by => lambda { |o| "#{o.manufacturer}-#{o.year}" } end # will scope Cars by for example "Ford-2016" # :manufacturer and :year should be Car's instance methods(like ActiveRecord columns) class Car < ActiveRecord::Base - protokoll :code, :scoped_by => Proc.new{ "#{manufacturer}-#{model}" } + protokoll :code, :scope_by => Proc.new{ "#{manufacturer}-#{model}" } end # will scope Cars by for example "Ford-Mustang", "Chevrolet-Camaro" ``` @@ -140,6 +140,10 @@ Run the generator rails g protokoll:migration +Optional: If scope_by will be used run next generator as well + + rails g protokoll:migration:scope_by + and migrate your database rake db:migrate diff --git a/lib/generators/protokoll/migration/scope_by_generator.rb b/lib/generators/protokoll/migration/scope_by_generator.rb new file mode 100644 index 0000000..679d4e3 --- /dev/null +++ b/lib/generators/protokoll/migration/scope_by_generator.rb @@ -0,0 +1,26 @@ +require 'rails/generators' + +module Protokoll + module Generators + module Migration + class ScopeByGenerator < ::Rails::Generators::Base + + include Rails::Generators::Migration + + desc "Generate protokoll's scope_by migration" + def create_migration_file + migration_name = "add_scope_by_to_custom_auto_increments.rb" + migration_template migration_name, File.join('db', 'migrate', migration_name) + end + + def self.source_root + @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates')) + end + + def self.next_migration_number(path) + Time.now.utc.strftime("%Y%m%d%H%M%S") + end + end + end + end +end diff --git a/lib/generators/protokoll/migration/templates/add_scope_by_to_custom_auto_increments.rb b/lib/generators/protokoll/migration/templates/add_scope_by_to_custom_auto_increments.rb new file mode 100644 index 0000000..d927195 --- /dev/null +++ b/lib/generators/protokoll/migration/templates/add_scope_by_to_custom_auto_increments.rb @@ -0,0 +1,12 @@ +class AddScopeByToCustomAutoIncrements < ActiveRecord::Migration + def up + add_column :custom_auto_increments, :counter_model_scope, :string + add_index :custom_auto_increments, [:counter_model_name, :counter_model_scope], + :unique => true, :name => :counter_model_name_scope + end + + def down + remove_index :custom_auto_increments, name: :counter_model_name_scope + remove_column :custom_auto_increments, :counter_model_scope, :string + end +end diff --git a/lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb b/lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb index 4525e81..93fba87 100644 --- a/lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb +++ b/lib/generators/protokoll/migration/templates/create_custom_auto_increments.rb @@ -2,13 +2,11 @@ class CreateCustomAutoIncrements < ActiveRecord::Migration def up create_table :custom_auto_increments, :force => true do |t| t.string :counter_model_name - t.string :counter_model_scope t.integer :counter, :default => 0 t.timestamps end add_index :custom_auto_increments, :counter_model_name - add_index :custom_auto_increments, [:counter_model_name, :counter_model_scope], :unique => true end def down diff --git a/test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb b/test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb index bf318bb..b559411 100644 --- a/test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb +++ b/test/dummy/db/migrate/20120222164124_create_custom_auto_increments.rb @@ -2,13 +2,11 @@ class CreateCustomAutoIncrements < ActiveRecord::Migration def up create_table :custom_auto_increments, :force => true do |t| t.string :counter_model_name - t.string :counter_model_scope t.integer :counter, :default => 0 t.timestamps end add_index :custom_auto_increments, :counter_model_name - add_index :custom_auto_increments, [:counter_model_name, :counter_model_scope], :unique => true, name: 'counter_model_name_by_scope' end def down diff --git a/test/dummy/db/migrate/20160310030821_add_scope_by_to_custom_auto_increments.rb b/test/dummy/db/migrate/20160310030821_add_scope_by_to_custom_auto_increments.rb new file mode 100644 index 0000000..d927195 --- /dev/null +++ b/test/dummy/db/migrate/20160310030821_add_scope_by_to_custom_auto_increments.rb @@ -0,0 +1,12 @@ +class AddScopeByToCustomAutoIncrements < ActiveRecord::Migration + def up + add_column :custom_auto_increments, :counter_model_scope, :string + add_index :custom_auto_increments, [:counter_model_name, :counter_model_scope], + :unique => true, :name => :counter_model_name_scope + end + + def down + remove_index :custom_auto_increments, name: :counter_model_name_scope + remove_column :custom_auto_increments, :counter_model_scope, :string + end +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 9f50e2e..c40daaa 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20120222164124) do +ActiveRecord::Schema.define(version: 20160310030821) do create_table "calls", force: :cascade do |t| t.string "number" @@ -21,13 +21,13 @@ create_table "custom_auto_increments", force: :cascade do |t| t.string "counter_model_name" - t.string "counter_model_scope" t.integer "counter", default: 0 t.datetime "created_at" t.datetime "updated_at" + t.string "counter_model_scope" end - add_index "custom_auto_increments", ["counter_model_name", "counter_model_scope"], name: "counter_model_name_by_scope", unique: true + add_index "custom_auto_increments", ["counter_model_name", "counter_model_scope"], name: "counter_model_name_scope", unique: true add_index "custom_auto_increments", ["counter_model_name"], name: "index_custom_auto_increments_on_counter_model_name" create_table "protocols", force: :cascade do |t|