hanami_id-generators 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aff23030c604ab832dbe7647994f4176b974f9b6385a436677b918f4db22c301
4
+ data.tar.gz: e9378b3562c2db3b8eb94e27d937b8ae66e90d052e0a8180622bdffc7dc500c0
5
+ SHA512:
6
+ metadata.gz: f4b4b4c06d5d855465c617948997f6db268ff04dc8e2753edb66c446cd39fcf08e1e0d99f33e3da562c818908f7593270d15b032bc10268450210a214bc3294d
7
+ data.tar.gz: 95ffefb906ae18061dce01ec12695898782195361a414af5162e6620b4284f31d774dbcfb5d46860726335e4669e8f71adc85a0465255986df7bd6ce1ce96d1d
@@ -0,0 +1,5 @@
1
+ # HanamiId::Generators
2
+
3
+ Generators for HanamiId - Authentication solution for [Hanami](https://github.com/hanami/hanami) framework.
4
+
5
+ Generates authentication app with all controller actions, views and templates.
@@ -0,0 +1,25 @@
1
+ # rubocop:disable Naming/FileName
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "hanami_id"
6
+ # TODO: remove pry
7
+ # require "pry-byebug"
8
+
9
+ module HanamiId
10
+ module Generators
11
+ class << self
12
+ attr_accessor :root
13
+ attr_accessor :templates
14
+ end
15
+
16
+ @root = Pathname.new File.expand_path(File.dirname(__dir__))
17
+ @templates = @root.join "lib", "hanami_id-generators", "templates"
18
+ end
19
+ end
20
+
21
+ require "hanami_id-generators/version"
22
+ require "hanami_id-generators/destroy/auth"
23
+ require "hanami_id-generators/generate/auth"
24
+
25
+ # rubocop:enable Naming/FileName
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli/commands"
4
+
5
+ module HanamiId
6
+ module Destroy
7
+ extend Hanami::CLI::Registry
8
+
9
+ class Auth < Hanami::CLI::Commands::Command
10
+ desc "Destroy an app with hanami_id"
11
+
12
+ option :app, default: "auth", desc: "Application name"
13
+ option :model, default: "user", desc: "User model name"
14
+ option :modules, type: :array, values: HanamiId::MODULES,
15
+ default: HanamiId.default_modules, desc: "List of modules"
16
+ option :mode, values: HanamiId::MODES, default: "standalone",
17
+ desc: "Level of itegration in project apps (callbacks, helpers etc.)"
18
+
19
+ example [
20
+ ""
21
+ ]
22
+
23
+ def call(app:, model:, mode:, **_options)
24
+ HanamiId.logger.info "Destroying #{app} app!"
25
+ HanamiId.model_name = model
26
+ Hanami::CLI::Commands::Destroy::App.new(
27
+ command_name: "destroy app"
28
+ ).call(app: app)
29
+
30
+ remove_lib_app_directory(app)
31
+ remove_default_migration
32
+ return unless mode == "project"
33
+
34
+ # remove_config
35
+ remove_initializer
36
+ end
37
+
38
+ private
39
+
40
+ def remove_lib_app_directory(app)
41
+ destination = project.root.join("lib", app)
42
+ return unless files.exist?(destination)
43
+
44
+ files.delete_directory destination
45
+ say(:remove, destination)
46
+ end
47
+
48
+ def remove_default_migration
49
+ migration_name = Hanami::Utils::Inflector.pluralize HanamiId.model_name
50
+ destination = Dir.glob(
51
+ project.root.join("db", "migrations", "*create_#{migration_name}.rb")
52
+ ).first
53
+ return unless files.exist?(destination)
54
+
55
+ files.delete(destination)
56
+ say(:remove, destination)
57
+ end
58
+
59
+ def remove_config
60
+ # TODO: remove project-wide integration
61
+ raise "Not implemented"
62
+ end
63
+
64
+ def remove_initializer
65
+ destination = File.join project.initializers, "hanami_id.rb"
66
+ return unless files.exist?(destination)
67
+
68
+ files.delete destination
69
+ say(:remove, destination)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ Hanami::CLI::Commands.register "destroy auth", HanamiId::Destroy::Auth
@@ -0,0 +1,208 @@
1
+ # rubocop:disable Metrics/ClassLength
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "hanami/cli/commands"
6
+
7
+ module HanamiId
8
+ module Generate
9
+ extend Hanami::CLI::Registry
10
+
11
+ class Auth < Hanami::CLI::Commands::Command
12
+ CONTROLLER_ACTIONS = {
13
+ "sessions" => {
14
+ "new" => "GET",
15
+ "create" => "POST",
16
+ "delete" => "DELETE"
17
+ },
18
+ "registrations" => {
19
+ "index" => "GET",
20
+ "new" => "GET",
21
+ "create" => "POST",
22
+ "edit" => "GET",
23
+ "show" => "GET",
24
+ "update" => "PUT",
25
+ "delete" => "DELETE"
26
+ }
27
+ }.freeze
28
+
29
+ desc "Generate app with hanami_id integration"
30
+
31
+ option :app, default: "auth", desc: "Application name"
32
+ option :model, default: "user", desc: "User model name"
33
+ option :modules, type: :array, values: HanamiId::MODULES,
34
+ default: HanamiId.default_modules, desc: "List of modules"
35
+ option :id_type, default: "integer", desc: "User model name"
36
+ option :login_column, default: "email", desc: "Login column in the DB"
37
+ option :password_column, default: "password_hash",
38
+ desc: "Password column in the DB"
39
+ option :mode, values: HanamiId::MODES, default: "standalone",
40
+ desc: "Level of itegration in project apps (callbacks, helpers etc.)"
41
+
42
+ example [
43
+ "--app auth --model account --modules registrations"
44
+ ]
45
+
46
+ attr_accessor :auth_templates
47
+ attr_accessor :context
48
+
49
+ def initialize(*args)
50
+ super
51
+ @auth_templates = HanamiId::Generators.templates
52
+ end
53
+
54
+ # rubocop:disable Metrics/AbcSize
55
+ def call(
56
+ app:, model:, modules:, login_column:, password_column:, id_type:,
57
+ mode:, **options
58
+ )
59
+ HanamiId.logger.info "Generating #{app} app!"
60
+ HanamiId.model_name = model
61
+ @context = Hanami::CLI::Commands::Context.new(
62
+ app: app,
63
+ model: HanamiId.model_name,
64
+ repository: HanamiId.repository_name,
65
+ modules: modules,
66
+ id_type: id_type,
67
+ login_column: login_column,
68
+ password_column: password_column,
69
+ options: options.merge(project: app)
70
+ )
71
+
72
+ generate_default_app
73
+ generate_default_entity
74
+ generate_default_repository
75
+ generate_default_migration
76
+ generate_default_actions(modules)
77
+ generate_default_routes
78
+ add_initializer(mode)
79
+ add_config if mode == "project"
80
+ inject_authentication_require
81
+ inject_warden_helper
82
+ end
83
+ # rubocop:enable Metrics/AbcSize
84
+
85
+ private
86
+
87
+ def generate_default_app
88
+ Hanami::CLI::Commands::Generate::App.new(
89
+ command_name: "generate app"
90
+ ).call(app: context.app)
91
+ end
92
+
93
+ def generate_default_routes
94
+ source = auth_templates.join("routes.erb").to_s
95
+ destination = project.app_routes(context)
96
+
97
+ generate_file(source, destination, context)
98
+ say(:create, destination)
99
+ end
100
+
101
+ def generate_default_entity
102
+ source = auth_templates.join("entity.erb").to_s
103
+ destination = project.entity(context)
104
+
105
+ generate_file(source, destination, context)
106
+ say(:create, destination)
107
+ end
108
+
109
+ def generate_default_repository
110
+ source = auth_templates.join("repository.erb").to_s
111
+ destination = project.repository(context)
112
+
113
+ generate_file(source, destination, context)
114
+ say(:create, destination)
115
+ end
116
+
117
+ def generate_default_migration
118
+ source = auth_templates.join("migration.erb").to_s
119
+ migration_name = Hanami::Utils::Inflector.pluralize HanamiId.model_name
120
+ destination = project.migration(
121
+ context.with(migration: "create_#{migration_name}")
122
+ )
123
+
124
+ generate_file(source, destination, context)
125
+ say(:create, destination)
126
+ end
127
+
128
+ def generate_default_actions(modules)
129
+ CONTROLLER_ACTIONS.each do |controller_name, actions|
130
+ actions.each do |action, verb|
131
+ next unless modules.include? controller_name
132
+
133
+ generate_action(controller_name, action, verb)
134
+ end
135
+ end
136
+ end
137
+
138
+ def generate_action(controller_name, action, verb)
139
+ HanamiId.logger.info(
140
+ "Generating #{verb} #{controller_name}##{action}"
141
+ )
142
+ Hanami::CLI::Commands::Generate::Action.new(
143
+ command_name: "generate action"
144
+ ).call(
145
+ app: context.app,
146
+ action: "#{controller_name}##{action}",
147
+ method: verb
148
+ )
149
+ # binding.pry
150
+ # action_code = generate_code(controller_name, action)
151
+ action_code = generate_code("registrations", "create")
152
+ destination = project.action(
153
+ context.with(
154
+ controller: controller_name,
155
+ action: action
156
+ )
157
+ )
158
+ files.inject_line_after(destination, /def call/, action_code)
159
+ say(:insert, destination)
160
+ end
161
+
162
+ def add_initializer(mode)
163
+ source = auth_templates.join("config.erb").to_s
164
+ destination = if mode == "project"
165
+ File.join project.initializers, "hanami_id.rb"
166
+ else
167
+ project.root.join(
168
+ "apps", context.app, "config", "hanami_id.rb"
169
+ )
170
+ end
171
+ generate_file(source, destination, context)
172
+ say(:create, destination)
173
+ end
174
+
175
+ def add_config
176
+ # TODO: add project-wide integration
177
+ raise "Not implemented"
178
+ end
179
+
180
+ def inject_authentication_require
181
+ content = "require \"hanami_id/authentication\""
182
+ destination = project.app_application(context)
183
+
184
+ files.inject_line_before(destination, "", content)
185
+ say(:insert, destination)
186
+ end
187
+
188
+ def inject_warden_helper
189
+ content = " include HanamiId::Warden::AppHelper"
190
+ destination = project.app_application(context)
191
+
192
+ files.inject_line_after_last(destination, /Application </, content)
193
+ say(:insert, destination)
194
+ end
195
+
196
+ def generate_code(controller_name, action)
197
+ template = auth_templates.join(
198
+ "controllers", controller_name, "#{action}.erb"
199
+ )
200
+ render(template.to_s, context)
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+ Hanami::CLI::Commands.register "generate auth", HanamiId::Generate::Auth
207
+
208
+ # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ HanamiId.configure do |config|
4
+ config.app_name = '<%= HanamiId.classify app %>'
5
+ config.model_name = '<%= model %>'
6
+ config.login_column = '<%= login_column %>'
7
+ config.password_column = '<%= password_column %>'
8
+ end
9
+
10
+ <%= HanamiId.app_name %>::Action.include HanamiId::Authentication
@@ -0,0 +1,20 @@
1
+ def initialize(operation: Interactors::Registrations::Create.new)
2
+ @operation = operation
3
+ end
4
+
5
+ params do
6
+ required(:<%= HanamiId.model_name %>).schema do
7
+ required(:email) { filled? & str? & format?(URI::MailTo::EMAIL_REGEXP) }
8
+ required(:password) { filled? & str? & size?(6..64) }
9
+ end
10
+ end
11
+
12
+ def call()
13
+ if params.valid?
14
+ <%= HanamiId.model_name %> = @operation.call(params[:<%= HanamiId.model_name %>])
15
+ params.env["warden"].set_user(<%= HanamiId.model_name %>)
16
+ redirect_to routes.root_path
17
+ else
18
+ self.status = 422
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= HanamiId.classify model %> < Hanami::Entity
4
+ end
@@ -0,0 +1,18 @@
1
+ require 'hanami/interactor'
2
+
3
+ module Interactors
4
+ module Registrations
5
+ class Create
6
+ include Hanami::Interactor
7
+
8
+ def initialize
9
+ # set up the object
10
+ end
11
+
12
+ def call(book_attributes)
13
+ <%= model %> = <%= HanamiId.classify model %>.new(params[:login], params[:password])
14
+ <%= HanamiId.classify repository %>.create(<%= model %>)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ Hanami::Model.migration do
2
+ up do
3
+ <%- if id_type == 'uuid' -%>
4
+ execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
5
+
6
+ <%- end -%>
7
+ create_table :<%= Hanami::Utils::Inflector.pluralize model %> do
8
+ <%- if id_type == 'uuid' -%>
9
+ primary_key :id, <%= id_type %>, null: false,
10
+ default: Hanami::Model::Sql.function(:uuid_generate_v4)
11
+ <%- else -%>
12
+ primary_key :id, null: false
13
+ <%- end -%>
14
+
15
+ column :<%= login_column %>, String, null: false
16
+ column :<%= password_column %>, String, null: false
17
+
18
+ column :created_at, DateTime, null: false
19
+ column :updated_at, DateTime, null: false
20
+ end
21
+ end
22
+
23
+ down do
24
+ drop_table :<%= Hanami::Utils::Inflector.pluralize model %>
25
+ <%- if id_type == 'uuid' -%>
26
+ execute 'DROP EXTENSION IF EXISTS "uuid-ossp"'
27
+ <%- end -%>
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= HanamiId.classify repository %> < Hanami::Repository
4
+ def authenticate(login, password)
5
+
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ <% if modules.include? 'sessions' -%>
2
+ resources :sessions, only: [:new, :create, :delete]
3
+ <% end -%>
4
+ <% if modules.include? 'registrations' -%>
5
+ resources :registrations
6
+ <% end -%>
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HanamiId
4
+ module Generators
5
+ VERSION = "0.1.1"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hanami_id-generators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Viacheslav Ptsarev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hanami
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: |
84
+ Generates authentication app with all controller actions, views and
85
+ templates.
86
+ email:
87
+ - leemour@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - README.md
93
+ - lib/hanami_id-generators.rb
94
+ - lib/hanami_id-generators/destroy/auth.rb
95
+ - lib/hanami_id-generators/generate/auth.rb
96
+ - lib/hanami_id-generators/templates/config.erb
97
+ - lib/hanami_id-generators/templates/controllers/registrations/create.erb
98
+ - lib/hanami_id-generators/templates/entity.erb
99
+ - lib/hanami_id-generators/templates/interactors/registrations/create.erb
100
+ - lib/hanami_id-generators/templates/migration.erb
101
+ - lib/hanami_id-generators/templates/repository.erb
102
+ - lib/hanami_id-generators/templates/routes.erb
103
+ - lib/hanami_id-generators/version.rb
104
+ homepage: https://github.com/leemour/hanami_id/gems/hanami_id-generators
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ homepage_uri: https://github.com/leemour/hanami_id/gems/hanami_id-generators
109
+ source_code_uri: https://github.com/leemour/hanami_id/gems/hanami_id-generators
110
+ bug_tracker_uri: https://github.com/leemour/hanami_id/gems/hanami_id-generators/issues
111
+ changelog_uri: https://github.com/leemour/hanami_id/gems/hanami_id-generators/CHANGELOG.md
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - "~>"
119
+ - !ruby/object:Gem::Version
120
+ version: '2.3'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Generators for HanamiId
131
+ test_files: []