effective_developer 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e4f396c9ab5a37dfca3a796aa10d204827c3e34de709bf9d1d8ce1db2af5201
4
- data.tar.gz: 4a6034049262d3f56c850dc6a2bc05de3f29201debd3b26ec3ae22851532bfa7
3
+ metadata.gz: 0db076604fcb663e2f5ad49b01da50837dfde6c9ae38b9be6d6ba3b265504a44
4
+ data.tar.gz: cd78feba26b117b151bdea4b0f6840d5bb01851b9d522d2e0df4bb68dd669f4e
5
5
  SHA512:
6
- metadata.gz: eaeec872eeb393fe930c08ea2a14d8ebd17af0e9df9a4ac2c7fdb21047b2888386c6d3b4f5afefd2ecd6e78d11dc81816c6ad6ae83e56e5af6877b4a6b903354
7
- data.tar.gz: ed971e8f872fa301fbb1dde42fac07f704f1e0eec46b53fb1250f634cc67e7787362a829d958f160c9084195f459daedd74d4fae6015f2012759318d33c49c64
6
+ metadata.gz: '0338a8bcac5b91c619acf7b4137c26a992a5398ee79444e50f47efee88051ba2bbf128998c095086de636fec797f246e778e9a2139a4644651b399e1c170863d'
7
+ data.tar.gz: 90f4c12d302a98c1be2e46da4b0d04bbc123953ddebd4864a8dd79d2837273ebf58a7492bd244fd9e78df3642286729d762dfb82f7a751e080c772001dd025ea
@@ -1,3 +1,3 @@
1
1
  module EffectiveDeveloper
2
- VERSION = '0.5.5'.freeze
2
+ VERSION = '0.6.0'.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,5 +1,40 @@
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
6
+
7
+ col :updated_at, visible: false
8
+ col :created_at, visible: false
9
+ col :id, visible: false
2
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
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
3
38
  datatable do
4
39
  order :updated_at
5
40
 
@@ -30,5 +65,5 @@ class <%= resource.namespaced_class_name.pluralize %>Datatable < Effective::Data
30
65
  collection do
31
66
  <%= resource.class_name %>.deep.all
32
67
  end
33
-
34
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 -%>
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.5
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-26 00:00:00.000000000 Z
11
+ date: 2021-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails