effective_developer 0.5.2 → 0.6.1

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
  SHA256:
3
- metadata.gz: 0deae13cd4fb9d39c7108dbe297cbccce12e308dc8510f4f974c634a7d4045ee
4
- data.tar.gz: 575191ece3ae021c25de70db77066cc0893cd7713707a63f1b700586c1a4b547
3
+ metadata.gz: fc26ec312df01c35b4f0541fbb8fee46f5b93c32c615cd3c1f0c8c659b67110a
4
+ data.tar.gz: 74a960c89033ea9531c3d0950e97d2e4bcac7c4d7a64ee4e9bc6cd1029e7ac51
5
5
  SHA512:
6
- metadata.gz: 103153bf5fe52387af807cb90b2855f17bfc490af84e2f1b7dcb058deac2b1e1901c5feb04a00119f66ad51db9363f44ad2224d346c570a251711f45143742c1
7
- data.tar.gz: b9b6041460328adad3dc4910f0548171a35fd89a1b518d8a515f18740f8238f6db62629a8ef5d7530babe3677799d409f44129a86651a89af7b9d8edca4a3094
6
+ metadata.gz: d63cb384af0910ac4f33e2696e4c72ab388fce049c31e35174bd940da6cbfc699e0bc5dcf821e72a60a1d000d6644b351ed231d9b5c5a180bcca8da7d71f5566
7
+ data.tar.gz: efeb52d66a9c083038609146ac5901ed8cd9521ff5ef74e0cd7537d4ac1fe2d9609095a2d79d998150612c7e2e52ce11ab1f377b824cd6bac21a574d62c1701b
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Effective::Profiler.allocations { my_method() }
4
+
5
+ module Effective
6
+ class Profiler
7
+
8
+ def self.allocations(sourcefiles: ['effective_'], &block)
9
+ raise('please install the allocation_stats gem') unless defined?(AllocationStats)
10
+
11
+ # Run block
12
+ retval = nil
13
+ stats = AllocationStats.trace { retval = yield(block) }
14
+
15
+ # Compute Allocations
16
+ allocations = stats.allocations.to_a
17
+
18
+ # Filter
19
+ if sourcefiles.present?
20
+ sourcefiles = Array(sourcefiles)
21
+ allocations = allocations.select! { |allocation| sourcefiles.any? { |str| allocation.sourcefile.include?(str) } }
22
+ end
23
+
24
+ # Sort
25
+ allocations = allocations.sort_by { |allocation| allocation.memsize }
26
+
27
+ # Print
28
+ puts AllocationStats::AllocationsProxy.new(allocations).to_text
29
+
30
+ puts "Total allocations: #{allocations.length}. Total size: #{allocations.sum(&:memsize)}"
31
+
32
+ retval
33
+ end
34
+
35
+
36
+ end
37
+
38
+ end
@@ -1,4 +1,5 @@
1
1
  require 'effective_resources'
2
+ require 'rails/generators'
2
3
  require 'generators/effective/helpers'
3
4
  require 'effective_developer/engine'
4
5
  require 'effective_developer/version'
@@ -1,3 +1,3 @@
1
1
  module EffectiveDeveloper
2
- VERSION = '0.5.2'.freeze
2
+ VERSION = '0.6.1'.freeze
3
3
  end
@@ -15,20 +15,26 @@ module Effective
15
15
 
16
16
  argument :actions, type: :array, default: ['crud'], banner: 'action action'
17
17
 
18
+ def validate_resource
19
+ exit unless resource_valid?
20
+ end
21
+
18
22
  def invoke_ability
19
23
  say_status :invoke, :ability, :white
20
24
  end
21
25
 
22
26
  def create_ability
23
- unless File.exists?('app/models/ability.rb')
27
+ unless File.exists?(resource.abilities_file)
24
28
  say_status(:skipped, :ability, :yellow) and return
25
29
  end
26
30
 
27
- Effective::CodeWriter.new('app/models/ability.rb') do |w|
28
- if w.find { |line, depth| depth == 2 && line == ability }
31
+ Effective::CodeWriter.new(resource.abilities_file) do |w|
32
+ if w.find { |line, depth| (depth == 2 || depth == 3) && line == ability }
29
33
  say_status :identical, ability, :blue
30
34
  else
31
- w.insert_into_first(ability) { |line, depth| line.start_with?('def initialize') }
35
+ w.insert_into_first(ability + "\n") { |line, depth| line.start_with?('def initialize') || line.end_with?('abilities(user)') }
36
+
37
+ say_status :ability, ability
32
38
  end
33
39
  end
34
40
  end
@@ -55,7 +61,13 @@ module Effective
55
61
  abilities = '[' + abilities.map { |action| ':' + action }.join(', ') + ']'
56
62
  end
57
63
 
58
- "can #{abilities}, #{resource.class_name}"
64
+ name = if resource.module_name.present?
65
+ resource.class_name.split('::').last
66
+ else
67
+ resource.class_name
68
+ end
69
+
70
+ "can #{abilities}, #{name}"
59
71
  )
60
72
  end
61
73
  end
@@ -17,6 +17,10 @@ module Effective
17
17
  argument :actions, type: :array, default: ['crud'], banner: 'action action'
18
18
  class_option :attributes, type: :array, default: [], desc: 'Included permitted params, otherwise read from model'
19
19
 
20
+ def validate_resource
21
+ exit unless resource_valid?
22
+ end
23
+
20
24
  def assign_actions
21
25
  @actions = invoked_actions
22
26
  end
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Generates a datatable
6
6
  # rails generate effective:datatable Thing
7
- # rails generate effective:controller Thing name:string description:text
7
+ # rails generate effective:datatable Thing name:string description:text
8
8
 
9
9
  module Effective
10
10
  module Generators
@@ -18,6 +18,10 @@ module Effective
18
18
  argument :actions, type: :array, default: ['crud'], banner: 'action action'
19
19
  class_option :attributes, type: :array, default: [], desc: 'Included permitted params, otherwise read from model'
20
20
 
21
+ def validate_resource
22
+ exit unless resource_valid?
23
+ end
24
+
21
25
  def assign_attributes
22
26
  @attributes = invoked_attributes.presence || resource_attributes(all: true)
23
27
  self.class.send(:attr_reader, :attributes)
@@ -32,7 +36,10 @@ module Effective
32
36
  say_status(:skipped, :datatable, :yellow) and return
33
37
  end
34
38
 
35
- template 'datatables/datatable.rb', resource.datatable_file
39
+ with_resource_tenant do
40
+ template 'datatables/datatable.rb', resource.datatable_file
41
+ end
42
+
36
43
  end
37
44
 
38
45
  end
@@ -20,6 +20,10 @@ module Effective
20
20
  argument :attributes, type: :array, default: [], banner: 'field[:type] field[:type]'
21
21
  class_option :tabbed, type: :string, default: 'true'
22
22
 
23
+ def validate_resource
24
+ exit unless resource_valid?
25
+ end
26
+
23
27
  def assign_attributes
24
28
  @attributes = invoked_attributes.presence || resource_attributes
25
29
  self.class.send(:attr_reader, :attributes)
@@ -30,15 +34,19 @@ module Effective
30
34
  end
31
35
 
32
36
  def create_flat_form
33
- if options[:tabbed] == 'false'
34
- template 'forms/flat/_form.html.haml', resource.view_file('form', partial: true)
37
+ with_resource_tenant do
38
+ if options[:tabbed] == 'false'
39
+ template 'forms/flat/_form.html.haml', resource.view_file('form', partial: true)
40
+ end
35
41
  end
36
42
  end
37
43
 
38
44
  def create_tabbed_form
39
- if options[:tabbed] == 'true'
40
- template 'forms/tabbed/_form.html.haml', resource.view_file('form', partial: true)
41
- template 'forms/tabbed/_form_resource.html.haml', resource.flat_view_file("form_#{resource.name}", partial: true)
45
+ with_resource_tenant do
46
+ if options[:tabbed] == 'true'
47
+ template 'forms/tabbed/_form.html.haml', resource.view_file('form', partial: true)
48
+ template 'forms/tabbed/_form_resource.html.haml', resource.view_file("form_#{resource.name}", partial: true)
49
+ end
42
50
  end
43
51
  end
44
52
 
@@ -4,6 +4,24 @@ module Effective
4
4
 
5
5
  protected
6
6
 
7
+ # This is kind of a validate for the resource
8
+ def resource_valid?
9
+ if resource.klass.blank?
10
+ say_status(:error, "Unable to find resource klass from #{name}", :red)
11
+ return false
12
+ end
13
+
14
+ true
15
+ end
16
+
17
+ def with_resource_tenant(&block)
18
+ if defined?(Tenant) && resource.tenant.present?
19
+ Tenant.as(resource.tenant) { yield }
20
+ else
21
+ yield
22
+ end
23
+ end
24
+
7
25
  def resource
8
26
  @resource ||= Effective::Resource.new(name)
9
27
  end
@@ -55,24 +73,26 @@ module Effective
55
73
  end
56
74
 
57
75
  def resource_attributes(all: false)
58
- klass_attributes = resource.klass_attributes(all: all)
76
+ with_resource_tenant do
77
+ klass_attributes = resource.klass_attributes(all: all)
59
78
 
60
- if klass_attributes.blank?
61
- if ActiveRecord::Migration.respond_to?(:check_pending!)
62
- pending = (ActiveRecord::Migration.check_pending! rescue true)
63
- else
64
- pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present?
65
- end
79
+ if klass_attributes.blank?
80
+ if ActiveRecord::Migration.respond_to?(:check_pending!)
81
+ pending = (ActiveRecord::Migration.check_pending! rescue true)
82
+ else
83
+ pending = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations(ActiveRecord::Migrator.migrations_paths)).pending_migrations.present?
84
+ end
66
85
 
67
- if pending
68
- migrate = ask("Unable to read the attributes of #{resource.klass || resource.name}. There are pending migrations. Run db:migrate now? [y/n]")
69
- system('bundle exec rake db:migrate') if migrate.to_s.include?('y')
86
+ if pending
87
+ migrate = ask("Unable to read the attributes of #{resource.klass || resource.name}. There are pending migrations. Run db:migrate now? [y/n]")
88
+ system('bundle exec rake db:migrate') if migrate.to_s.include?('y')
89
+ end
90
+
91
+ klass_attributes = resource.klass_attributes(all: all)
70
92
  end
71
93
 
72
- klass_attributes = resource.klass_attributes(all: all)
94
+ klass_attributes.presence || resource.model_attributes(all: all)
73
95
  end
74
-
75
- klass_attributes.presence || resource.model_attributes(all: all)
76
96
  end
77
97
 
78
98
  end
@@ -13,6 +13,10 @@ module Effective
13
13
 
14
14
  desc 'Adds a menu link to an existing _navbar.html.haml'
15
15
 
16
+ def validate_resource
17
+ exit unless resource_valid?
18
+ end
19
+
16
20
  def invoke_menu
17
21
  say_status :invoke, :menu, :white
18
22
  end
@@ -22,7 +26,7 @@ module Effective
22
26
  return unless resource.namespaces.blank?
23
27
 
24
28
  begin
25
- Effective::CodeWriter.new('app/views/layouts/_navbar.html.haml') do |w|
29
+ Effective::CodeWriter.new(resource.menu_file) do |w|
26
30
  if w.find { |line, _| line == menu_content.second.strip }
27
31
  say_status :identical, menu_path, :blue
28
32
  else
@@ -44,7 +48,7 @@ module Effective
44
48
  return unless resource.namespaces == ['admin']
45
49
 
46
50
  begin
47
- Effective::CodeWriter.new('app/views/layouts/_navbar_admin.html.haml') do |w|
51
+ Effective::CodeWriter.new(resource.admin_menu_file) do |w|
48
52
  if w.find { |line, _| line == menu_content.second.strip }
49
53
  say_status :identical, menu_path, :blue
50
54
  else
@@ -72,7 +76,8 @@ module Effective
72
76
  end
73
77
 
74
78
  def menu_path
75
- [resource.namespace, resource.plural_name, 'path'].compact * '_'
79
+ path = [resource.namespace, resource.plural_name, 'path'].compact * '_'
80
+ resource.tenant.present? ? "#{resource.tenant}.#{path}" : path
76
81
  end
77
82
  end
78
83
  end
@@ -16,21 +16,47 @@ module Effective
16
16
  desc 'Creates a migration.'
17
17
 
18
18
  argument :attributes, type: :array, default: [], banner: 'field[:type] field[:type]'
19
+ class_option :database, type: :string, desc: "Database to generate the migration for"
20
+
21
+ def validate_resource
22
+ exit unless resource_valid?
23
+ end
19
24
 
20
25
  def invoke_migration
21
26
  say_status :invoke, :migration, :white
22
27
  end
23
28
 
29
+ # rails generate effective:migration courses body:text --database example
24
30
  def create_migration
25
31
  if invoked_attributes.present?
26
- Rails::Generators.invoke('migration', ["create_#{plural_name}"] + (invokable(invoked_attributes) | timestamps))
27
- elsif resource.klass_attributes.present?
28
- raise 'klass_attributes already exist. We cant migrate (yet). Exiting.'
29
- elsif resource.model_attributes.present?
30
- Rails::Generators.invoke('migration', ["create_#{plural_name}"] + invokable(resource.model_attributes))
31
- else
32
- raise 'You need to specify some attributes or have a model file present'
32
+ args = ["create_#{plural_name}"] + (invokable(invoked_attributes) | timestamps)
33
+ args += ["--database", options['database']] if options['database']
34
+ Rails::Generators.invoke('migration', args)
35
+ return
36
+ end
37
+
38
+ return if with_resource_tenant do
39
+ table_name = resource.klass.table_name
40
+
41
+ if ActiveRecord::Base.connection.table_exists?(table_name)
42
+ say_status(:error, "#{table_name} table already exist. We can't migrate (yet). Exiting.", :red)
43
+ true
44
+ end
33
45
  end
46
+
47
+ if resource.model_attributes.blank?
48
+ say_status(:error, "No model attributes present. Please add the effective_resource do ... end block and try again", :red)
49
+ return
50
+ end
51
+
52
+ args = ["create_#{plural_name}"] + invokable(resource.model_attributes) - timestamps
53
+ args += ["--database", options['database']] if options['database']
54
+
55
+ if options['database'].blank? && defined?(Tenant)
56
+ args += ["--database", resource.klass.name.split('::').first.downcase]
57
+ end
58
+
59
+ Rails::Generators.invoke('migration', args)
34
60
  end
35
61
 
36
62
  protected
@@ -15,6 +15,10 @@ module Effective
15
15
 
16
16
  argument :actions, type: :array, default: ['crud'], banner: 'action action'
17
17
 
18
+ def validate_resource
19
+ exit unless resource_valid?
20
+ end
21
+
18
22
  def invoke_route
19
23
  say_status :invoke, :route, :white
20
24
  end
@@ -22,7 +26,7 @@ module Effective
22
26
  def create_route
23
27
  blocks = []
24
28
 
25
- Effective::CodeWriter.new('config/routes.rb') do |w|
29
+ Effective::CodeWriter.new(resource.routes_file) do |w|
26
30
  resource.namespaces.each do |namespace|
27
31
  index = nil
28
32
 
@@ -54,7 +58,7 @@ module Effective
54
58
 
55
59
  def resources
56
60
  @resources ||= (
57
- resources = "resources :#{plural_name}"
61
+ resources = "resources :#{resource.plural_name}"
58
62
 
59
63
  if ((crud_actions - ['show']) == invoked_actions)
60
64
  resources << ', except: [:show]'
@@ -16,6 +16,10 @@ module Effective
16
16
  argument :actions, type: :array, default: ['crud'], banner: 'action action'
17
17
  class_option :attributes, type: :array, default: [], desc: 'Included form attributes, otherwise read from model'
18
18
 
19
+ def validate_resource
20
+ exit unless resource_valid?
21
+ end
22
+
19
23
  def assign_attributes
20
24
  @attributes = (invoked_attributes.presence || resource_attributes).except(:archived)
21
25
  self.class.send(:attr_reader, :attributes)
@@ -1,3 +1,11 @@
1
- class <%= resource.namespaced_class_name.pluralize %>Controller < <%= [resource.namespace.try(:classify).presence, ApplicationController].compact.join('::') %>
1
+ <% if resource.module_name.present? -%>
2
+ module <%= resource.module_name %>
3
+ class <%= resource.module_namespaced %>Controller < <%= resource.module_namespace %>ApplicationController
4
+ include Effective::CrudController
5
+ end
6
+ end
7
+ <% else -%>
8
+ class <%= resource.namespaced_class_name.pluralize %>Controller < <%= resource.module_namespace %>ApplicationController
2
9
  include Effective::CrudController
3
10
  end
11
+ <% end -%>
@@ -1,14 +1,43 @@
1
- class <%= resource.namespaced_class_name.pluralize %>Datatable < Effective::Datatable
1
+ <% if resource.module_name.present? -%>
2
+ module <%= resource.module_name %>
3
+ class <%= resource.module_namespaced %>Datatable < Effective::Datatable
4
+ datatable do
5
+ order :updated_at
2
6
 
3
- bulk_actions do
4
- bulk_action 'Delete selected', <%= [resource.namespaces, resource, 'path'].flatten.compact.join('_') %>(:ids), data: { method: :delete, confirm: 'Really delete selected?' }
5
- end
7
+ col :updated_at, visible: false
8
+ col :created_at, visible: false
9
+ col :id, visible: false
10
+
11
+ <% if resource.belong_tos.present? || resource.has_anys.present? -%>
12
+ <% resource.belong_tos.each do |reference| -%>
13
+ col :<%= reference.name %>
14
+ <% end -%>
15
+ <% resource.has_anys.each do |reference| -%>
16
+ col :<%= reference.name %>
17
+ <% end -%>
18
+
19
+ <% end -%>
20
+ <% attributes.except(:created_at, :updated_at, :id, :archived).each do |name, _| -%>
21
+ col :<%= name %>
22
+ <% end -%>
23
+ <% if attributes.key?(:archived) -%>
24
+
25
+ col :archived, search: { value: false }
26
+ <% end -%>
27
+
28
+ actions_col
29
+ end
6
30
 
31
+ collection do
32
+ <%= resource.class_name %>.deep.all
33
+ end
34
+ end
35
+ end
36
+ <% else -%>
37
+ class <%= resource.namespaced_class_name.pluralize %>Datatable < Effective::Datatable
7
38
  datatable do
8
39
  order :updated_at
9
40
 
10
- bulk_actions_col
11
-
12
41
  col :updated_at, visible: false
13
42
  col :created_at, visible: false
14
43
  col :id, visible: false
@@ -36,5 +65,5 @@ class <%= resource.namespaced_class_name.pluralize %>Datatable < Effective::Data
36
65
  collection do
37
66
  <%= resource.class_name %>.deep.all
38
67
  end
39
-
40
68
  end
69
+ <% end -%>
@@ -1,6 +1,6 @@
1
1
  = tabs do
2
2
  = tab '<%= resource.human_name.titleize %>' do
3
- = render '/<%= resource.plural_name %>/form_<%= resource.name %>', <%= resource.name %>: <%= resource.name %>, namespace: <%= resource.namespace ? ":#{resource.namespace}" : 'nil' %>
3
+ = render '<%= resource.view_file_path("form_#{resource.name}") %>', <%= resource.name %>: <%= resource.name %>
4
4
 
5
5
  <%- if resource.nested_resources.present? || resource.instance.respond_to?(:log_changes_datatable) %>
6
6
  - if <%= resource.name %>.persisted?
@@ -1,4 +1,8 @@
1
- = effective_form_with(model: [(namespace if defined?(namespace)), <%= resource.name %>]) do |f|
1
+ <% if resource.namespace.present? -%>
2
+ = effective_form_with(model: [:<%= resource.namespace %>, <%= resource.name %>]) do |f|
3
+ <% else -%>
4
+ = effective_form_with(model: <%= resource.name %>) do |f|
5
+ <% end -%>
2
6
  <% resource.belong_tos.each do |reference| -%>
3
7
  <%= render_field(reference, depth: 1) %>
4
8
  <% end -%>
@@ -4,14 +4,35 @@ namespace :pg do
4
4
  # bundle exec rake pg:pull
5
5
  # bundle exec rake pg:pull[staging]
6
6
  # bundle exec rake pg:pull[158.204.33.124]
7
+
8
+ # bundle exec rake pg:pull
9
+ # bundle exec rake pg:pull[staging]
10
+ # bundle exec rake pg:pull[158.204.33.124]
11
+ # bundle exec rake pg:pull filename=latest.dump database=example
12
+ # DATABASE=example bundle exec rake pg:load
7
13
  desc 'Creates a new backup on remote server and downloads it to latest.dump'
8
14
  task :pull, [:remote] => :environment do |t, args|
15
+ defaults = { database: nil, filename: (ENV['DATABASE'] || 'latest') + '.dump' }
16
+ env_keys = { database: ENV['DATABASE'], filename: ENV['FILENAME'] }
17
+ keywords = ARGV.map { |a| a.split('=') if a.include?('=') }.compact.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
18
+ args.with_defaults(defaults.compact.merge(env_keys.compact).merge(keywords))
19
+
20
+ # Validate Config
21
+ config = ActiveRecord::Base.configurations[Rails.env]
22
+ configs = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env)
23
+
24
+ if configs.length > 1 && args.database.blank?
25
+ puts "Multiple database configs exist for #{Rails.env} environment."
26
+ puts "Please run bundle exec rake pg:pull database=x"
27
+ puts "Where x is one of: #{configs.map { |config| config.name }.to_sentence}"
28
+ exit
29
+ end
9
30
 
10
31
  # Heroku mode
11
32
  if `git remote -v | grep heroku`.length > 0
12
33
  args.with_defaults(remote: 'heroku')
13
34
 
14
- puts "=== Pulling remote '#{args.remote}' database into latest.dump"
35
+ puts "=== Pulling remote '#{args.remote}' #{args.database} database into #{args.filename}"
15
36
 
16
37
  # Create a backup on heroku
17
38
  unless system("heroku pg:backups:capture --remote #{args.remote}")
@@ -19,12 +40,12 @@ namespace :pg do
19
40
  end
20
41
 
21
42
  # Download it to local
22
- unless system("curl -o latest.dump `heroku pg:backups:public-url --remote #{args.remote}`")
43
+ unless system("curl -o #{args.filename} `heroku pg:backups:public-url --remote #{args.remote}`")
23
44
  abort("Error downloading database")
24
45
  end
25
46
 
26
47
  # Load it
27
- Rake::Task['pg:load'].invoke
48
+ Rake::Task['pg:load'].invoke(*args)
28
49
  exit
29
50
  end
30
51
 
@@ -36,24 +57,24 @@ namespace :pg do
36
57
  user: ENV['HATCHBOX_USER'] || 'deploy'
37
58
  )
38
59
 
39
- puts "=== Pulling hatchbox '#{args.remote}' #{args.app} database into latest.dump"
60
+ puts "=== Pulling hatchbox '#{args.remote}' #{args.app} #{args.database} database into #{args.filename}"
40
61
 
41
62
  # SSH into hatchbox and call rake pg:save there to create latest.dump
42
- unless(result = `ssh #{args.user}@#{args.remote} << EOF
63
+ unless(result = `ssh -T #{args.user}@#{args.remote} << EOF
43
64
  cd ~/#{args.app}/current/
44
- bundle exec rake pg:save[latest.dump]
65
+ bundle exec rake pg:save database=#{args.database} filename=#{args.filename}
45
66
  `).include?('Saving database completed') # The output of pg:save down below
46
67
  puts("Error calling ssh #{args.user}@#{args.remote} and running rake pg:save on hatchbox from ~/#{args.app}/current/")
47
68
  abort(result)
48
69
  end
49
70
 
50
71
  # SCP to copy the hatchkbox latest.dump to local
51
- unless system("scp #{args.user}@#{args.remote}:~/#{args.app}/current/latest.dump ./")
72
+ unless system("scp #{args.user}@#{args.remote}:~/#{args.app}/current/#{args.filename} ./")
52
73
  abort("Error downloading database")
53
74
  end
54
75
 
55
76
  # Load it
56
- Rake::Task['pg:load'].invoke
77
+ Rake::Task['pg:load'].invoke(*args)
57
78
  exit
58
79
  end
59
80
 
@@ -66,24 +87,48 @@ namespace :pg do
66
87
  #
67
88
  # bundle exec rake pg:load => Will replace the current database with latest.dump
68
89
  # bundle exec rake pg:load[something.dump] => Will replace the current database with something.dump
90
+ # bundle exec rake pg:load filename=latest.dump database=example
91
+ # DATABASE=example bundle exec rake pg:load
69
92
  desc 'Loads a postgresql .dump file into the development database (latest.dump by default)'
70
- task :load, [:file_name] => :environment do |t, args|
71
- args.with_defaults(:file_name => 'latest.dump')
93
+ task :load, [:filename] => :environment do |t, args|
94
+ defaults = { database: nil, filename: (ENV['DATABASE'] || 'latest') + '.dump' }
95
+ env_keys = { database: ENV['DATABASE'], filename: ENV['FILENAME'] }
96
+ keywords = ARGV.map { |a| a.split('=') if a.include?('=') }.compact.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
97
+ args.with_defaults(defaults.compact.merge(env_keys.compact).merge(keywords))
98
+
99
+ # Validate filename
100
+ unless File.exists?(Rails.root + args.filename)
101
+ puts "#{args.filename || none} does not exist"; exit
102
+ end
72
103
 
104
+ # Validate Config
73
105
  config = ActiveRecord::Base.configurations[Rails.env]
74
- db = { username: (config['username'] || `whoami`), password: config['password'], host: config['host'], port: (config['port'] || 5432), database: config['database'] }
106
+ configs = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env)
75
107
 
76
- puts "=== Loading #{args.file_name} into local '#{db[:database]}' database"
108
+ if configs.length > 1 && args.database.blank?
109
+ puts "Multiple database configs exist for #{Rails.env} environment."
110
+ puts "Please run bundle exec rake pg:load database=x"
111
+ puts "Where x is one of: #{configs.map { |config| config.name }.to_sentence}"
112
+ exit
113
+ end
77
114
 
78
- # bin/rails db:environment:set RAILS_ENV=development
79
- if Rails.env != 'production'
80
- ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = '1'
115
+ if configs.length > 1 && args.database.present?
116
+ config = configs.find { |config| config.name == args.database }
81
117
  end
82
118
 
83
- Rake::Task['db:drop'].invoke
84
- Rake::Task['db:create'].invoke
119
+ if config.blank?
120
+ puts "Unable to find Rails database config for #{Rails.env}. Exiting."; exit
121
+ end
85
122
 
86
- if system("export PGPASSWORD=#{db[:password]}; pg_restore --no-acl --no-owner --clean --if-exists -h #{db[:host]} -U #{db[:username]} -d #{db[:database]} #{args.file_name}")
123
+ config = config.configuration_hash if config.respond_to?(:configuration_hash)
124
+ config = config.stringify_keys
125
+
126
+ db = { username: (config['username'] || `whoami`), password: config['password'], host: config['host'], port: (config['port'] || 5432), database: config['database'] }
127
+ db.transform_values! { |v| v.respond_to?(:chomp) ? v.chomp : v }
128
+
129
+ puts "=== Loading #{args.filename} into local '#{db[:database]}' database"
130
+
131
+ if system("export PGPASSWORD=#{db[:password]}; pg_restore --no-acl --no-owner --clean --if-exists -h #{db[:host]} -U #{db[:username]} -d #{db[:database]} #{args.filename}")
87
132
  puts "Loading database completed"
88
133
  else
89
134
  abort "Error loading database"
@@ -93,8 +138,11 @@ namespace :pg do
93
138
  # bundle exec rake pg:save => Will dump the database to latest.dump
94
139
  # bundle exec rake pg:save[something.dump] => Will dump the database to something.dump
95
140
  desc 'Saves the development database to a postgresql .dump file (latest.dump by default)'
96
- task :save, [:file_name] => :environment do |t, args|
97
- args.with_defaults(:file_name => 'latest.dump')
141
+ task :save, [:filename] => :environment do |t, args|
142
+ defaults = { database: nil, filename: (ENV['DATABASE'] || 'latest') + '.dump' }
143
+ env_keys = { database: ENV['DATABASE'], filename: ENV['FILENAME'] }
144
+ keywords = ARGV.map { |a| a.split('=') if a.include?('=') }.compact.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
145
+ args.with_defaults(defaults.compact.merge(env_keys.compact).merge(keywords))
98
146
 
99
147
  db = if ENV['DATABASE_URL'].to_s.length > 0
100
148
  uri = URI.parse(ENV['DATABASE_URL']) rescue nil
@@ -102,13 +150,36 @@ namespace :pg do
102
150
 
103
151
  { username: uri.user, password: uri.password, host: uri.host, port: (uri.port || 5432), database: uri.path.sub('/', '') }
104
152
  else
153
+ # Validate Config
105
154
  config = ActiveRecord::Base.configurations[Rails.env]
155
+ configs = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env)
156
+
157
+ if configs.length > 1 && args.database.blank?
158
+ puts "Multiple database configs exist for #{Rails.env} environment."
159
+ puts "Please run bundle exec rake pg:save database=x"
160
+ puts "Where x is one of: #{configs.map { |config| config.name }.to_sentence}"
161
+ exit
162
+ end
163
+
164
+ if configs.length > 1 && args.database.present?
165
+ config = configs.find { |config| config.name == args.database }
166
+ end
167
+
168
+ if config.blank?
169
+ puts "Unable to find Rails database config for #{Rails.env}. Exiting."; exit
170
+ end
171
+
172
+ config = config.configuration_hash if config.respond_to?(:configuration_hash)
173
+ config = config.stringify_keys
174
+
106
175
  { username: (config['username'] || `whoami`.chomp), password: config['password'], host: config['host'], port: (config['port'] || 5432), database: config['database'] }
107
176
  end
108
177
 
109
- puts "=== Saving local '#{db[:database]}' database to #{args.file_name}"
178
+ db.transform_values! { |v| v.respond_to?(:chomp) ? v.chomp : v }
179
+
180
+ puts "=== Saving local '#{db[:database]}' database to #{args.filename}"
110
181
 
111
- if system("export PGPASSWORD=#{db[:password]}; pg_dump -Fc --no-acl --no-owner -h #{db[:host]} -p #{db[:port]} -U #{db[:username]} #{db[:database]} > #{args.file_name}")
182
+ if system("export PGPASSWORD=#{db[:password]}; pg_dump -Fc --no-acl --no-owner -h #{db[:host]} -p #{db[:port]} -U #{db[:username]} #{db[:database]} > #{args.filename}")
112
183
  puts "Saving database completed"
113
184
  else
114
185
  abort "Error saving database"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_developer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-30 00:00:00.000000000 Z
11
+ date: 2021-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -51,6 +51,7 @@ files:
51
51
  - app/models/effective/code_writer.rb
52
52
  - app/models/effective/csv_importer.rb
53
53
  - app/models/effective/live_generator.rb
54
+ - app/models/effective/profiler.rb
54
55
  - config/effective_developer.rb
55
56
  - lib/effective_developer.rb
56
57
  - lib/effective_developer/engine.rb
@@ -103,7 +104,7 @@ homepage: https://github.com/code-and-effect/effective_developer
103
104
  licenses:
104
105
  - MIT
105
106
  metadata: {}
106
- post_install_message:
107
+ post_install_message:
107
108
  rdoc_options: []
108
109
  require_paths:
109
110
  - lib
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
120
  version: '0'
120
121
  requirements: []
121
122
  rubygems_version: 3.1.2
122
- signing_key:
123
+ signing_key:
123
124
  specification_version: 4
124
125
  summary: Provides some quality of life developer tools.
125
126
  test_files: []