kadim 0.1.2 → 0.2.0

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: 6e28919fd08424ba733eb143d20dfe0e26736893f9ec3d9d284502bc5cbd9c9a
4
- data.tar.gz: 02bb8d94e7cd1b4e79917a090eecc53623fa0c60fed2d63d7d5abdf2e58a6ca5
3
+ metadata.gz: 371d7c712488e0bd60536420e63236cfb37e7c97b90450e39a58d8f83839d327
4
+ data.tar.gz: 69b09fd55aee468869ef81e198b7457feaaff206afbafef3dc9fabc0f74f8674
5
5
  SHA512:
6
- metadata.gz: 8ff6119ceaa400e9c72c2faa391d7c39cd750baa2a3e6baef9ee5802682b4504bb1e581a20c12033a0a2aed33fff98be7ceb98c9b030cd6fdd981942a791d188
7
- data.tar.gz: 5d145ad9cb9e3655ae248c9c844455f030c207d9897a49305cdefa7125f70a5ce3cb90fe17600161b8230fe457fd6954dd291fc0822f80503dd9a5cdfb1e78c6
6
+ metadata.gz: 90e882c703bafbed037d2b6f664b1e49b19ec4f132be0f98226485a56e781e40ea04318a20ab65138adc9900bd581c061af139a3cc067771cd0c6ad6dbaea6c5
7
+ data.tar.gz: 2b9a1636ea547f41eb6fd76063f9dbc737790dbcc2e5b598a57270796f614b3797706fd0c1caad95e38aa8d22bb71ba151c4afdb6ba653673fb234eca599db44
data/README.md CHANGED
@@ -25,10 +25,26 @@ ActiveRecord methods added by gems are ignored.
25
25
  Files are generated in `tmp/kadim` and loaded into memory, including views. This also has the advantage of allowing the
26
26
  gem to work in environments with ephemeral file systems.
27
27
 
28
- If you want to customize `Kadim::ApplicationController`, the basic controller for all generated controllers, you can
29
- copy the file [app/controllers/kadim/application_controller.rb](https://github.com/fnix/kadim/blob/master/app/controllers/kadim/application_controller.rb)
30
- to your application. You can do the same for [app/views/layouts/kadim/application.html.erb](https://github.com/fnix/kadim/blob/master/app/views/layouts/kadim/application.html.erb).
31
- If you want to customize the generated controller/views just copy them from `tmp/kadim` to your application.
28
+ ## Customization
29
+
30
+ You can manually copy kadim files and put them in the same path within your application. You will probably want to do
31
+ this with the main kadim controller, to add safety rules, etc.
32
+
33
+ You also provide two generators to make this task easier.
34
+
35
+ ```bash
36
+ rails g kadim: host
37
+ rails g kadim: host: scaffold_controller ModelName
38
+ ```
39
+
40
+ The first one copies the basic kadim infrastructure to your application, ie the
41
+ [main controller](app/controllers/kadim/application_controller.rb), the
42
+ [main view](app/views/kadim/application/index.html.erb) and its [layout](app/views/layouts/kadim/application.html.erb),
43
+ a [helper](app/helpers/kadim/application_helper.rb) and [assets](app/assets/).
44
+
45
+ The second copies the files that kadim dynamically generates into your application, allowing you to have full control
46
+ over controller and views implementation. This generator is a thin layer over the Rails scaffold_controller, but it only
47
+ accepts the model name as a parameter, for attributes all fields of the model are used.
32
48
 
33
49
  ## Installation
34
50
  Add this line to your application's Gemfile:
@@ -47,9 +63,16 @@ Or install it yourself as:
47
63
  $ gem install kadim
48
64
  ```
49
65
 
66
+ Mount the engine:
67
+ ```ruby
68
+ mount Kadim::Engine, at: '/kadim'
69
+ ```
70
+
71
+ And access http://localhost:3000/kadim
72
+
50
73
  ## Roadmap
51
74
  - [x] Dynamic CRUD generation from application models
52
- - [ ] Tasks to copy files form kadim to the hosted application
75
+ - [x] Tasks to copy files form kadim to the hosted application
53
76
  - [ ] Add support to ActiveStorage attachments
54
77
  - [ ] Add support to belongs_to relationships
55
78
  - [ ] Add a beautiful look and feel
@@ -3,7 +3,7 @@
3
3
  module Kadim
4
4
  module ApplicationHelper
5
5
  def menu_links
6
- links = Kadim.app_model_names.map(&:camelize).map(&:constantize).map do |model_klass|
6
+ links = Kadim.app_model_paths.map(&:camelize).map(&:constantize).map do |model_klass|
7
7
  link_to model_klass.model_name.human(count: :many), model_klass
8
8
  end
9
9
  safe_join(links, " | ")
data/config/routes.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Kadim::Engine.routes.draw do
4
- Kadim.app_model_names.map(&:tableize).each do |table_name|
4
+ Kadim.app_model_paths.map(&:tableize).each do |table_name|
5
5
  resources table_name
6
6
  end
7
7
  root "application#index"
@@ -0,0 +1,16 @@
1
+ Description:
2
+ Hosts on your application the files generated in runtime by `kadim` for the giving resource. This turns the
3
+ responsability for the resource to your application.
4
+
5
+ The model attributes are automatically detected by `kadim`.
6
+ resource.
7
+
8
+ Example:
9
+ `rails generate kadim:host:scaffold_controller CreditCard`
10
+
11
+ This will create:
12
+ Credit card controller with URLs like /credit_cards.
13
+ Controller: app/controllers/credit_cards_controller.rb
14
+ Test: test/controllers/credit_cards_controller_test.rb
15
+ Views: app/views/credit_cards/index.html.erb [...]
16
+ Helper: app/helpers/credit_cards_helper.rb
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kadim
4
+ module Host
5
+ class ScaffoldControllerGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def scaffold_controller
9
+ model_path = name.underscore
10
+ unless Kadim.app_model_paths.include?(model_path)
11
+ puts "Are you sure \"#{name}\" is a model?"
12
+ return
13
+ end
14
+
15
+
16
+ Rails::Generators.namespace = Kadim
17
+ generator = Rails::Generators::ScaffoldControllerGenerator.new(
18
+ [model_path, *Kadim.scaffold_attributes(model_path.camelize.constantize)],
19
+ ["--no-jbuilder", "--template-engine=erb"],
20
+ behavior: behavior,
21
+ destination_root: destination_root
22
+ )
23
+
24
+ # jbuilder 2.9.1 controller template uses a method removed from Rails6
25
+ # https://github.com/rails/jbuilder/issues/470
26
+ source_path_idx = generator.class.source_paths.index { |source_path| source_path.include?("jbuilder") }
27
+ generator.class.source_paths[source_path_idx] = generator.class.source_root if source_path_idx
28
+
29
+ generator.invoke_all
30
+ end
31
+ end
32
+ end
33
+ end
data/lib/kadim/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kadim
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/kadim.rb CHANGED
@@ -1,17 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rails/generators"
4
- require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"
5
-
6
3
  require "kadim/engine"
7
4
  require "kadim/template/memory_resolver"
8
5
 
9
6
  module Kadim
10
- def self.app_model_names
7
+ def self.app_model_paths
11
8
  Dir[Rails.root.join("app", "models", "**", "*.rb")]
12
9
  .reject { |model_path| model_path.include?("/concerns/") || model_path.include?("application_record") }
13
10
  .map { |model_path| model_path.remove(%r{.*/app/models/}, ".rb") }
14
- .select { |model_name| model_name.camelize.constantize.try(:table_exists?) }
11
+ .select { |model_path| model_path.camelize.constantize.try(:table_exists?) }
15
12
  .sort
16
13
  end
17
14
 
@@ -20,6 +17,15 @@ module Kadim
20
17
  scaffold_controllers
21
18
  end
22
19
 
20
+ def self.scaffold_attributes(model_klass)
21
+ model_klass.columns
22
+ .reject { |column| %w[id created_at updated_at].include?(column.name) }
23
+ .sort_by(&:name)
24
+ .map { |column| [column.name, column.type] }
25
+ .to_h
26
+ .map { |k, v| "#{k}:#{v}" }
27
+ end
28
+
23
29
  class << self
24
30
  private
25
31
  def cleanup
@@ -41,13 +47,16 @@ module Kadim
41
47
  end
42
48
 
43
49
  def controller_filenames
44
- app_model_names.map { |model_name| "#{model_name.pluralize}_controller" }
50
+ app_model_paths.map { |model_name| "#{model_name.pluralize}_controller" }
45
51
  end
46
52
 
47
53
  def scaffold_controllers
54
+ Rails.application.load_generators
55
+ require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"
56
+
48
57
  current_namespace = Rails::Generators.namespace
49
58
  Rails::Generators.namespace = Kadim
50
- app_model_names.each { |model_name| scaffold_controller(model_name) }
59
+ app_model_paths.each { |model_name| scaffold_controller(model_name) }
51
60
  load_kadim_controllers
52
61
  load_kadim_views
53
62
  ensure
@@ -60,21 +69,14 @@ module Kadim
60
69
 
61
70
  generator = Rails::Generators::ScaffoldControllerGenerator.new(
62
71
  [model_name, *scaffold_attributes(model_klass)],
63
- ["--force", "--quiet", "--no-orm", "--no-test-framework", "--no-helper"],
72
+ ["--force", "--quiet", "--no-test-framework", "--no-helper"],
64
73
  destination_root: Rails.root.join("tmp", "kadim")
65
74
  )
75
+ source_path_idx = generator.class.source_paths.index { |source_path| source_path.include?("jbuilder") }
76
+ generator.class.source_paths[source_path_idx] = generator.class.source_root if source_path_idx
66
77
  generator.invoke_all
67
78
  end
68
79
 
69
- def scaffold_attributes(model_klass)
70
- model_klass.columns
71
- .reject { |column| %w[id created_at updated_at].include?(column.name) }
72
- .sort_by(&:name)
73
- .map { |column| [column.name, column.type] }
74
- .to_h
75
- .map { |k, v| "#{k}:#{v}" }
76
- end
77
-
78
80
  def load_kadim_controllers
79
81
  Dir[Rails.root.join("tmp", "kadim", "app", "controllers", "**", "*_controller.rb")].each { |path| load path }
80
82
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kadim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kadu Diógenes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2019-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activestorage-resumable
@@ -52,6 +52,62 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.29.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: generator_spec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.16.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.16.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.7.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.7.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: nokogiri
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.10.5
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.10.5
55
111
  - !ruby/object:Gem::Dependency
56
112
  name: pry
57
113
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +136,20 @@ dependencies:
80
136
  - - "~>"
81
137
  - !ruby/object:Gem::Version
82
138
  version: 4.2.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: rails-dom-testing
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 2.0.3
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 2.0.3
83
153
  - !ruby/object:Gem::Dependency
84
154
  name: rspec-rails
85
155
  requirement: !ruby/object:Gem::Requirement
@@ -214,6 +284,8 @@ files:
214
284
  - config/routes.rb
215
285
  - lib/generators/kadim/host/USAGE
216
286
  - lib/generators/kadim/host/host_generator.rb
287
+ - lib/generators/kadim/host/scaffold_controller/USAGE
288
+ - lib/generators/kadim/host/scaffold_controller/scaffold_controller_generator.rb
217
289
  - lib/kadim.rb
218
290
  - lib/kadim/engine.rb
219
291
  - lib/kadim/template/memory_resolver.rb