railsless-active_record 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f122a117e6b93dbe3c7349d10e6161421bbf8dd7
4
- data.tar.gz: 2503c67f693b02ab67decdaff8aa982d4e02d9d9
3
+ metadata.gz: 440df35df9bfdf54c4f3ac5a448cb874c5718d7c
4
+ data.tar.gz: c5b1ca4083c3e97d41e58511fd8b6eb87a2eee00
5
5
  SHA512:
6
- metadata.gz: bb0e33c4b0de00d44b34545acccb0b96628186f89b08cb0804ac998a0f502493b9d1046f8dc0211c61693f0c64121b475920434d10a782e5d66e9bcf93388cc2
7
- data.tar.gz: 398ea7f655fa43bf16c90c857cd73b1ed4fa422110e0ae9f0085364fadc0b88ce43ffbcaec8cc26225767ba1086d221c33751e9067bc0b350b480165ab3bb819
6
+ metadata.gz: 6ae4b00085195b98cc491925c85d7f1b5cadeb4f001dd8119e465cabf63e306acec196ad250bc74e4c19ed5d34c1b448f47739b8b9c2e072ce6e0ad222869732
7
+ data.tar.gz: 9db05790b5d871eaf44becea098d2ab0117b8ba0d78987c2120688798e759d0e917803b64f06a527f5c0d8396bac298e3865fbcc69b0a4bb1e1b40eee9f881fc
@@ -6,7 +6,7 @@ require 'erb'
6
6
  module Railsless
7
7
  module ActiveRecord
8
8
  class Config
9
- def self.attr_accessor_with_default(name, &block)
9
+ def self.accessor_with_default(name, &block)
10
10
  module_eval do
11
11
  attr_writer name
12
12
  define_method(name) do
@@ -28,11 +28,11 @@ module Railsless
28
28
  end
29
29
 
30
30
  # Fumble around picking the right running environment.
31
- attr_accessor_with_default(:env) do
31
+ accessor_with_default(:env) do
32
32
  ENV['RACK_ENV'] || ENV['SINATRA_ENV'] || ENV['RAILS_ENV'] || ENV['ENV'] || 'development'
33
33
  end
34
34
 
35
- attr_accessor_with_default(:db_config) do
35
+ accessor_with_default(:db_config) do
36
36
  case
37
37
  when File.exist?(db_config_path)
38
38
  YAML.load(read_config(db_config_path))
@@ -43,15 +43,15 @@ module Railsless
43
43
  end
44
44
  end
45
45
 
46
- attr_accessor_with_default(:db_config_path) do
47
- File.join(root, 'config', 'database.yml')
48
- end
49
- attr_accessor_with_default(:db_path) { File.join(root, 'db') }
50
- attr_accessor_with_default(:seeds_path) { File.join(db_path, 'seeds.rb') }
51
- attr_accessor_with_default(:schema_path) { File.join(db_path, 'schema.rb') }
52
- attr_accessor_with_default(:migrations_path) { File.join(db_path, 'migrate') }
46
+ accessor_with_default(:config_path) { File.join(root, 'config') }
47
+ accessor_with_default(:db_config_path) { File.join(config_path, 'database.yml') }
48
+
49
+ accessor_with_default(:db_path) { File.join(root, 'db') }
50
+ accessor_with_default(:seeds_path) { File.join(db_path, 'seeds.rb') }
51
+ accessor_with_default(:schema_path) { File.join(db_path, 'schema.rb') }
52
+ accessor_with_default(:migrations_path) { File.join(db_path, 'migrate') }
53
53
 
54
- attr_accessor_with_default(:logger) { Logger.new(STDOUT) }
54
+ accessor_with_default(:logger) { Logger.new(STDOUT) }
55
55
 
56
56
  protected
57
57
 
@@ -1,6 +1,8 @@
1
+ require 'active_support/inflector' # Doesn't mix in; called for its class methods.
1
2
  require 'active_record'
2
- require 'rake'
3
3
  require 'fileutils'
4
+ require 'rake'
5
+ require 'erb'
4
6
 
5
7
  module Railsless
6
8
  module ActiveRecord
@@ -50,21 +52,33 @@ module Railsless
50
52
 
51
53
  desc "Generate and write a config/database.yml"
52
54
  task :config do
53
- config_path = config.db_config_path
54
- if File.exists?(config_path)
55
- puts "Database config already exists at #{config_path}; skipping..."
55
+ db_config_path = config.db_config_path
56
+ if File.exists?(db_config_path)
57
+ puts "Database config already exists at #{db_config_path}; skipping..."
56
58
  else
57
- FileUtils.cp(
58
- File.join(TEMPLATES_PATH, 'database.yml'),
59
- config_path
60
- )
59
+ FileUtils.mkdir_p(config.config_path)
60
+ FileUtils.cp(File.join(TEMPLATES_PATH, 'database.yml'), db_config_path)
61
61
  end
62
62
  end
63
63
 
64
64
  desc "Generate a database migration, eg: rake db:generate:migration NAME=CreatePosts"
65
65
  task :migration do
66
- # TODO: NAME=...
67
- raise "Not Implemented"
66
+ migrations_path = config.migrations_path
67
+ timestamp = Time.now.strftime("%Y%m%d%H%M%S")
68
+ name = ENV.fetch('NAME') do
69
+ fail "Usage: rake db:generate:migration NAME=CreatePosts"
70
+ end
71
+
72
+ # Normalise the name to a MigrationClass and a migration_filename
73
+ migration_class = ActiveSupport::Inflector.camelize(name)
74
+ migration_filename = "#{timestamp}_#{ActiveSupport::Inflector.underscore(name)}.rb"
75
+ migration_path = File.join(migrations_path, migration_filename)
76
+
77
+ template = File.read(File.join(TEMPLATES_PATH, 'migration.erb'))
78
+ FileUtils.mkdir_p(migrations_path)
79
+ File.write(migration_path, ERB.new(template).result(binding))
80
+
81
+ puts "Created migration: #{migration_path}"
68
82
  end
69
83
  end
70
84
  end
@@ -1,5 +1,5 @@
1
1
  module Railsless
2
2
  module ActiveRecord
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -26,6 +26,9 @@ shared_examples "a Sinatra app" do
26
26
  end
27
27
  end
28
28
 
29
+ # TODO: This block is both testing integration AND the Rake tasks themselves.
30
+ # Break this out so we have independent Rake integration tests and a
31
+ # very set of tests, just to make sure it hooks up fine with Sinatra.
29
32
  context "generators" do
30
33
  describe "db:generate:config" do
31
34
  let(:config_path) do
@@ -42,7 +45,7 @@ shared_examples "a Sinatra app" do
42
45
  end
43
46
 
44
47
  it "generates a config file that doesn't yet exist" do
45
- FileUtils.rm_f(config_path)
48
+ FileUtils.rm_f(config_path) # TODO: Avoid directly modifying a fixture app like this.
46
49
  out = run "bundle exec rake db:generate:config"
47
50
  expect(File.read(config_path)).to eq File.read(template_path)
48
51
  end
@@ -54,8 +57,36 @@ shared_examples "a Sinatra app" do
54
57
  end
55
58
  end
56
59
 
57
- pending "'db:generate:migration NAME=NameHere' creates a migration" do
58
- # TODO: 'NameHere' -> 'name_here' transform.
60
+ describe "db:generate:migration NAME=NameHere" do
61
+ let(:migrations_path) { File.join(app_path, 'db', 'migrate') }
62
+
63
+ it "explodes when no name given" do
64
+ out = run 'bundle exec rake db:generate:migration', should_succeed=false
65
+ expect(out).to match /Usage:/
66
+ end
67
+
68
+ it "normalises NAME=CamelCase" do
69
+ out = run 'bundle exec rake db:generate:migration NAME=CreateExamples'
70
+ expect_migration(out, 'CreateExamples', 'create_examples')
71
+ end
72
+
73
+ it "normalises NAME=snake_case" do
74
+ out = run 'bundle exec rake db:generate:migration NAME=drop_examples'
75
+ expect_migration(out, 'DropExamples', 'drop_examples')
76
+ end
77
+
78
+ def expect_migration(out, class_name, filename_suffix)
79
+ expect(out).to match /\ACreated migration: (.*)\Z/ do |m|
80
+ filename = m[1]
81
+ expect(filename).to match /\/[0-9]+_#{filename_suffix}.rb\Z/
82
+ expect(File.read(filename)).to match /\Aclass #{class_name} < ActiveRecord::Migration/
83
+ end
84
+ end
85
+
86
+ # Clean up examples after each run.
87
+ after do
88
+ Dir["#{migrations_path}/*_examples.rb"].each {|f| FileUtils.rm_f(f) }
89
+ end
59
90
  end
60
91
  end
61
92
 
@@ -87,11 +118,11 @@ shared_examples "a Sinatra app" do
87
118
 
88
119
  # Helpers
89
120
 
90
- def run(command)
121
+ def run(command, should_succeed=true)
91
122
  output = ""
92
123
  Bundler.with_clean_env do
93
- output, status = Open3.capture2(command, :chdir => app_path)
94
- expect(status).to be_success
124
+ output, status = Open3.capture2e(command, :chdir => app_path)
125
+ expect(status.success?).to eq should_succeed
95
126
  end
96
127
  output
97
128
  end
@@ -0,0 +1,5 @@
1
+ class <%= migration_class %> < ActiveRecord::Migration
2
+ def change
3
+
4
+ end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railsless-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Howard
@@ -101,6 +101,7 @@ files:
101
101
  - spec/support/fs_fixture_helpers.rb
102
102
  - spec/support/inline_timeout_helpers.rb
103
103
  - templates/database.yml
104
+ - templates/migration.erb
104
105
  homepage: https://github.com/damncabbage/railsless-active_record
105
106
  licenses:
106
107
  - Apache License, Version 2.0