casey_jones 0.0.113 → 0.0.114

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate extra_user_attributes Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,38 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/generated_attribute'
4
+ require 'rails/generators/active_record'
5
+
6
+ class ExtraUserAttributesGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ require File.expand_path('../migration_emitter', __FILE__)
10
+
11
+ def manifest
12
+ self.class_name = "User"
13
+ self.emitter = MigrationEmitter.entity(:users)
14
+ self.emitter.column :confirmation_token, :string
15
+ self.emitter.column :confirmed_at, :datetime
16
+ self.emitter.column :confirmation_sent_at, :datetime
17
+ self.emitter.column :failed_attempts, :integer, :default=>0
18
+ self.emitter.column :unlock_token, :string
19
+ self.emitter.column :locked_at, :datetime
20
+ self.emitter.column :authentication_token, :string
21
+ self.emitter.column :login, :string
22
+ self.emitter.column :name, :string
23
+
24
+ migration_template 'add_fields_migration.rb', "db/migrate/add_fields_to_users"
25
+
26
+ end
27
+ attr_accessor :emitter, :class_name
28
+
29
+ def self.next_migration_number(dirname)
30
+ if ActiveRecord::Base.timestamped_migrations
31
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
32
+ else
33
+ "%.3d" % (current_migration_number(dirname) + 1)
34
+ end
35
+ end
36
+
37
+ end
38
+
@@ -0,0 +1,79 @@
1
+ class MigrationEmitter
2
+ attr_accessor :table_name
3
+ attr_accessor :columns
4
+ attr_accessor :options
5
+
6
+ def initialize(table_name, initial = [], options={})
7
+ self.table_name = table_name
8
+ self.columns = initial
9
+ self.options = options
10
+ end
11
+
12
+ def self.join_table(*classes)
13
+ a = self.new classes.map_to(:to_s).map_to(:tableize).sort.join('_').to_sym
14
+ (0..1).each {|i|
15
+ a.column classes[i].foreign_key, :integer, :index=>:true
16
+ }
17
+ a.options[:id] = false
18
+ a
19
+ end
20
+
21
+ def self.entity(class_name, *initial)
22
+ return self.new(class_name.to_s.tableize.to_sym, initial)
23
+ end
24
+
25
+ def column(name, datatype, options={})
26
+ self.columns << {:name=>name, :datatype=>datatype, :options=>options}
27
+ end
28
+
29
+ def create_table
30
+ return "
31
+ create_table #{create_table_def} do |t|
32
+ #{columns.map{|c| coldef(c)}.join("\n ")}
33
+ t.timestamps
34
+ end
35
+ " + create_indexes
36
+ end
37
+
38
+ def drop_table
39
+ "drop_table #{table_name.inspect}"
40
+ end
41
+
42
+ def add_columns
43
+ columns.map{|c|
44
+ "add_column " +
45
+ [table_name.inspect, c[:name].inspect, c[:datatype].inspect,
46
+ map_inspect(c[:options].except(:index))
47
+ ].flatten.join(', ')
48
+ }.join("\n ") + "\n " + create_indexes
49
+ end
50
+
51
+ def remove_columns
52
+ columns.map{|c| "remove_column #{table_name.inspect}, #{c[:name].inspect}"}.join("\n ")
53
+ end
54
+
55
+
56
+ private
57
+
58
+ def create_indexes
59
+ columns.select{|t| t[:options][:index] }.map{|t|
60
+ "add_index :#{table_name}, :#{t[:name]}" + (t[:options][:index]==:unique ? ", :unique=>true" : "")
61
+ }.join('\n ')
62
+ end
63
+
64
+ def coldef(col)
65
+ ["t.#{col[:datatype]} :#{col[:name]}",
66
+ map_inspect(col[:options].except(:index))].
67
+ flatten.join(', ')
68
+ end
69
+
70
+ def create_table_def
71
+ [table_name.inspect, map_inspect(options)].
72
+ flatten.join(", ")
73
+ end
74
+
75
+ def map_inspect(map)
76
+ map.map{|a,b| "#{a.inspect} => #{b.inspect}"}
77
+ end
78
+ end
79
+
@@ -0,0 +1,10 @@
1
+ class AddFieldsTo<%= class_name.pluralize.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ <%= emitter.add_columns %>
4
+ end
5
+
6
+ def self.down
7
+ <%= emitter.remove_columns %>
8
+ end
9
+ end
10
+
@@ -0,0 +1,10 @@
1
+ class CreateTable<%= class_name.pluralize.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ <%= emitter.create_table %>
4
+ end
5
+
6
+ def self.down
7
+ <%= emitter.drop_table %>
8
+ end
9
+ end
10
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casey_jones
3
3
  version: !ruby/object:Gem::Version
4
- hash: 253
4
+ hash: 251
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 113
10
- version: 0.0.113
9
+ - 114
10
+ version: 0.0.114
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyler Gannon
@@ -45,6 +45,11 @@ files:
45
45
  - lib/generators/anaf_habtm/anaf_habtm_generator.rb
46
46
  - lib/generators/anaf_habtm/assets/nested_attributes.css
47
47
  - lib/generators/anaf_habtm/assets/nested_attributes.js
48
+ - lib/generators/extra_user_attributes/USAGE
49
+ - lib/generators/extra_user_attributes/extra_user_attributes_generator.rb
50
+ - lib/generators/extra_user_attributes/migration_emitter.rb
51
+ - lib/generators/extra_user_attributes/templates/add_fields_migration.rb
52
+ - lib/generators/extra_user_attributes/templates/create_table_migration.rb
48
53
  - lib/generators/loadbehind/USAGE
49
54
  - lib/generators/loadbehind/loadbehind_generator.rb
50
55
  - lib/generators/loadbehind/templates/destroy.js.haml