Skip to content

Commit

Permalink
Add scope_by generator and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
emaxi committed Mar 10, 2016
1 parent c7a0271 commit 1c4e296
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions lib/generators/protokoll/migration/scope_by_generator.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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|
Expand Down

0 comments on commit 1c4e296

Please sign in to comment.