platforms-core 0.1.2 → 0.1.3

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: 63ca19d9929e26203496937d12e14635c4581e55c4f46df446f64c09c7236729
4
- data.tar.gz: ddab12ef1680d7ec7ff8df9099f05f662a9a0ee483719aa13b58a89920147a5a
3
+ metadata.gz: 635a8202983e8824756c06d3d17d240c0f5b7de960da2d962fb566511a8ed517
4
+ data.tar.gz: 24b2cbf4024362c01ecb502ea933279740ed30f8b758cd0c9d1199f09f4e7685
5
5
  SHA512:
6
- metadata.gz: d8043793edf1f427b4db0f4e96cf335f01803eeec53fd9cda8fde45992ad2da9d63b6bcebd35303c4e014ea7e7e5528a55ed4948d33e45369be7a92d02e9aba4
7
- data.tar.gz: 6505d451a04186dc268e80f0b7048ed1667eaba1323ecb78b530c69a166f0a12f7ea159207dec332bc43c23eef4f1ad831e896e879ab5ea14d7970be48189424
6
+ metadata.gz: 3051fc09751fc8f5e0d61fe38f8ed6ff08cca2c87b328416c3f62cab6cdfdde4acb58cfc48b9ddc3bf8df78fc2027dffafbfaac83ea520ac84145132e158829f
7
+ data.tar.gz: f7294f4060c51e25fffbccaba08010c0baef619f10796b8c621770ca5314a50ea7569c4a3d90c1497e2fe6e25453b33545f77a52e2e5d60f9720bb414374571e
data/README.md CHANGED
@@ -32,55 +32,88 @@ Or install it yourself as:
32
32
  $ gem install platforms-core
33
33
  ```
34
34
 
35
- Once the gem is installed, from your Rails directory you will need to install the gem's migrations:
35
+ Once the gem is installed, from your Rails directory you will can run the following generator to complete the installation:
36
36
 
37
37
  ```bash
38
- $ rake platforms_core:install:migrations
38
+ $ rails generate platforms:core:install
39
39
  ```
40
40
 
41
+ This will:
42
+
43
+ * Copy a basic initializer to `config/initializers/platforms_core.rb`; and
44
+ * Install the gem's migrations into your application.
45
+
41
46
  ## Configuration
42
47
 
43
48
  REST-based APIs require authentication to get started. Please refer to
44
49
  your platform for instructions on how to do this.
45
50
 
46
- The gem assumes that you have a class called `User` and a class called `Network` which might look something like the following:
51
+ ### Starting a New App
52
+
53
+ Your application needs to have at least `Network` and `User` models. These can be created by calling the generator:
54
+
55
+ ```bash
56
+ $ rails generate platform:core:network foo some_info:string
57
+ $ rails generate platform:core:user bar user more_info:string
58
+ ```
59
+
60
+ Most of the options for the regular ActiveRecord model generator are available, including namespacing and indexing.
61
+
62
+ This is roughly equivalent to calling the standard Rails model generators (`rails g model foo some_info:string`), however by using the above version the resulting models are configured by Platforms::Core as the `Network` or `User` models.
63
+
64
+ Typically these would be called "Network" and "User", but here we have called them "Foo" and "Bar".
65
+
66
+ ### Adding to an Existing App
67
+
68
+ If you already have `Network` and `User` models (which let's assume are called "Foo" and "Bar" respectively), you can add the relevant configuration by using the generator with the `--existing-model` flag:
69
+
70
+ ```bash
71
+ $ rails generate platform:core:network foo --existing-model
72
+ $ rails generate platform:core:user bar --existing-model
73
+ ```
74
+
75
+ This will add the relevant concerns to the models, and update the initializer, without needing to create models from scratch.
76
+
77
+ ### Manual Configuration
78
+
79
+ Finally, if you don't want to use the built-in generators then you can always create the models and configuration manually. The models should look something like this for a `Network`:
47
80
 
48
81
  ```ruby
49
- class User < ApplicationRecord
82
+ # app/models/network.rb
50
83
 
51
- # A User belongs to a Platforms::User
52
- belongs_to :platforms_user,
53
- class_name: "Platforms::User",
54
- inverse_of: :app_user
84
+ class Network < ApplicationRecord
85
+ include Platforms::Core::AppNetwork
86
+
87
+ # This requires an integer database column called
88
+ # 'platforms_network_id'
55
89
 
56
- # Ensure a User only maps to one Platforms::User
57
- validates :platforms_user_id, uniqueness: true
90
+ # ...
58
91
  end
59
92
  ```
60
- and Network:
93
+
94
+ and for a `User`:
61
95
 
62
96
  ```ruby
63
- class Network < ApplicationRecord
97
+ # app/models/user.rb
98
+
99
+ class User < ApplicationRecord
100
+ include Platforms::Core::AppUser
64
101
 
65
- # A Network belongs to a Platforms::Network
66
- belongs_to :platforms_network,
67
- class_name: "Platforms::Network",
68
- inverse_of: :app_network
102
+ # This requires an integer database column called
103
+ # 'platforms_user_id'
69
104
 
70
- # Ensure a Network only maps to one Platforms::Network
71
- validates :platforms_network_id, uniqueness: true
105
+ # ...
72
106
  end
73
107
  ```
74
108
 
75
- If you need the User and Network classes to be named differently,
76
- use configurations:
109
+ Additionally, you will need to create an initializer which configures the `Network` and `User` classes for the gem:
77
110
 
78
111
  ```ruby
79
112
  # config/initializers/platforms.rb
80
113
 
81
114
  Platforms::Core.configure do |config|
82
- config.network_class = "SampleNetwork"
83
- config.user_class = "SampleUser"
115
+ config.network_class = "Foo"
116
+ config.user_class = "Bar"
84
117
  end
85
118
  ```
86
119
 
@@ -0,0 +1,96 @@
1
+ require "rails/generators/active_record"
2
+
3
+ module Platforms
4
+ module Core
5
+ # Generators to simpify installing Platforms::Core
6
+ module Generators
7
+
8
+ # These are common functions that apply to both User and Network model types.
9
+ # The setup for both is really quite similar:
10
+ # * Generating a model, or creating a migration to add an association column
11
+ # * Add the relevant Concern to the Model
12
+ # * Update the initializer to reference the new class
13
+ class Base < Rails::Generators::NamedBase
14
+ include ActiveRecord::Generators::Migration
15
+
16
+ source_root File.join(__dir__, "templates")
17
+
18
+ argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
19
+
20
+ # Don't do check_class_collision here. It can be done within
21
+ # create_model instead, if required.
22
+
23
+ class_option :migration, type: :boolean
24
+ class_option :timestamps, type: :boolean
25
+ class_option :parent, type: :string, desc: "The parent class for the generated model"
26
+ class_option :indexes, type: :boolean, default: true, desc: "Add indexes for references and belongs_to columns"
27
+ class_option :primary_key_type, type: :string, desc: "The type for primary key"
28
+ class_option :database, type: :string, aliases: %i(--db), desc: "The database for your model's migration. By default, the current environment's primary database is used."
29
+
30
+ # Platforms-specific class option
31
+ class_option :existing_model, type: :boolean, default: false, desc: "Use existing model. Do not generate a new model definition"
32
+
33
+ # Create the model, unless --existing_model is specified.
34
+ # Calls the standard rails model generator, so should accept similar arguments
35
+ # to 'rails g model Foo'.
36
+ def create_model
37
+ return if options[:existing_model]
38
+ args = [name]
39
+ args << "platforms_#{concern_type}_id:integer"
40
+
41
+ # Recreate arguments
42
+ attributes.each do |a|
43
+ args << "#{a.name}:#{a.type}#{a.has_index? ? ":index" : "" }"
44
+ end
45
+
46
+ # Recreate options
47
+ options.each do |k, v|
48
+ unless k == "existing_model"
49
+ args << "--#{k}=#{v}"
50
+ end
51
+ end
52
+
53
+ # Use the standard model generator
54
+ generate "model", args.join(" ")
55
+ end
56
+
57
+ # Create a migration, when a model already exists.
58
+ # This could be for platforms_network_id or platforms_user_id.
59
+ def create_migration
60
+ return unless options[:existing_model]
61
+ migration_name = table_name.classify.pluralize
62
+ args = "AddPlatformsNetworkIdTo#{migration_name} platforms_#{concern_type}_id:integer"
63
+ generate "migration", args
64
+ end
65
+
66
+ # Adds the relevant concern to the network or user model.
67
+ # This is either Platforms::Core:AppNetwork or Platforms::Core::AppUser
68
+ # #file_name is inherited from Rails::Generators::NamedBase, according
69
+ # to https://guides.rubyonrails.org/generators.html
70
+ def add_concern_to_model
71
+
72
+ # Add the concern line
73
+ model_file_path = File.join("app", "models", class_path, "#{file_name}.rb")
74
+ inject_into_class model_file_path, class_name do
75
+ " include Platforms::Core::App#{concern_type.capitalize}\n\n"
76
+ end
77
+ end
78
+
79
+ # Set a line in the initializer to 'config.network_class = "Network"'
80
+ # or similar. This could also be 'config.user_class = "MyUser"'
81
+ #
82
+ # Rather than gsub, matching config lines are removed and then added again.
83
+ def edit_initializer
84
+ init = "config/initializers/platforms_core.rb"
85
+ gsub_file init, /\s*config.#{concern_type}_class\s+= .*\n/, "\n"
86
+
87
+ inject_into_file init, after: "Platforms::Core.configure do |config|\n" do
88
+ " config.#{concern_type}_class = \"#{class_name}\"\n"
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,11 @@
1
+ Description:
2
+ This will install the required migrations, and set up the initializer
3
+
4
+ Example:
5
+ rails generate platforms:core:install
6
+
7
+ This will create:
8
+ The initializer in config/initializers/platforms_core.rb
9
+ Run $ rake patforms_core:install:migrations
10
+
11
+ Note that it will not run the migrations
@@ -0,0 +1,26 @@
1
+ require 'rails/generators'
2
+
3
+ module Platforms
4
+ module Core
5
+
6
+ # Simplify the installation of Platforms::Core by creating an
7
+ # initializer file and installing the migrations.
8
+ # This does not run the migrations.
9
+ class InstallGenerator < Rails::Generators::Base
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ # Create config/initializers/platforms_core.rb according to the template.
13
+ def copy_initializer_file
14
+ copy_file "platforms_core.rb", "config/initializers/platforms_core.rb"
15
+ end
16
+
17
+ # Install the gem's migrations.
18
+ # This is equivalent to the built-in rake task
19
+ # "platforms_core:install:migrations"
20
+ def install_migrations
21
+ rake "platforms_core:install:migrations"
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # This file has been generated by the installer
2
+ # $ rails generate platforms:core:install
3
+ #
4
+ # To change the Network and User class, edit this file.
5
+ # It will also be automatically updated when either of the
6
+ # following generators are run:
7
+ # $ rails generate platforms:core:network [args]
8
+ # $ rails generate platforms:core:user [args]
9
+
10
+ Platforms::Core.configure do |config|
11
+ config.network_class = "Network"
12
+ config.user_class = "User"
13
+ end
@@ -0,0 +1,14 @@
1
+ Description:
2
+ Create a Network model, and include the relevant Platforms::Core Concern.
3
+ This will also set the relevant Network configuration in
4
+ config/initializers/plaforms_core.rb.
5
+
6
+ Example:
7
+ rails generate platforms:core:network blah more_info:string
8
+
9
+ This will create the Blah model, including migration, and test suite.
10
+ It will have a string column called "more_info".
11
+ The concern is then included in app/models/blah.rb
12
+
13
+ Also works for namespaced models, and accepts similar arguments
14
+ as the ActiveRecord generator (database columns, associations).
@@ -0,0 +1,19 @@
1
+ # require "platforms/core/generators/base"
2
+ require "generators/platforms/core/base"
3
+
4
+ module Platforms
5
+ module Core
6
+
7
+ # Create a model, with Platforms::Core specific additions for the
8
+ # 'Network' class that should be implemented by the parent
9
+ # application.
10
+ class NetworkGenerator < Platforms::Core::Generators::Base
11
+
12
+ # Set the concern_type to network
13
+ def concern_type
14
+ "network"
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ Description:
2
+ Create a User model, and include the relevant Platforms::Core Concern.
3
+ This will also set the relevant User configuration in
4
+ config/initializers/plaforms_core.rb.
5
+
6
+ Example:
7
+ rails generate platforms:core:user blah more_info:string
8
+
9
+ This will create the Blah model, including migration, and test suite.
10
+ It will have a string column called "more_info".
11
+ The concern is then included in app/models/blah.rb
12
+
13
+ Also works for namespaced models, and accepts similar arguments
14
+ as the ActiveRecord generator (database columns, associations).
@@ -0,0 +1,19 @@
1
+ #require "platforms/core/generators/base"
2
+ require "generators/platforms/core/base"
3
+
4
+ module Platforms
5
+ module Core
6
+
7
+ # Create a model, with Platforms::Core specific additions for the
8
+ # 'User' class that should be implemented by the parent
9
+ # application.
10
+ class UserGenerator < Platforms::Core::Generators::Base
11
+
12
+ # Set the concern_type to user.
13
+ def concern_type
14
+ "user"
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -1,8 +1,12 @@
1
+ require "platforms/core/app_network"
2
+ require "platforms/core/app_user"
1
3
  require "platforms/core/configuration"
2
4
  require "platforms/core/engine"
3
5
  require "platforms/core/omni_auth_setup"
4
6
  require "platforms/core/o_auth_2"
5
7
 
8
+ require "generators/platforms/core/install/install_generator"
9
+
6
10
  # The top level namespace for Platforms, which includes Core and
7
11
  # other vendor-specific implementations.
8
12
  module Platforms
@@ -0,0 +1,19 @@
1
+ module Platforms
2
+ module Core
3
+ # Add the relevant associations to a Network-type class.
4
+ module AppNetwork
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ # A valid association object is required from Rails 5 onwards,
9
+ # unless otherwise specified.
10
+ belongs_to :platforms_network,
11
+ class_name: "Platforms::Network",
12
+ inverse_of: :app_network
13
+
14
+ # Ensure AppNetwork only maps to one Platforms::Network
15
+ validates :platforms_network_id, uniqueness: true
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Platforms
2
+ module Core
3
+ # Add the relevant associations to a User-type class.
4
+ module AppUser
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ # A valid association object is required from Rails 5 onwards,
9
+ # unless otherwise specified.
10
+ belongs_to :platforms_user,
11
+ class_name: "Platforms::User",
12
+ inverse_of: :app_user
13
+
14
+ # Ensure AppUser only maps to one Platforms::User
15
+ validates :platforms_user_id, uniqueness: true
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,7 +3,6 @@ module Platforms
3
3
 
4
4
  # This is a module for common authentication methods, across multiple
5
5
  # platforms.
6
- # @todo extract out the common functionality.
7
6
  module OAuth2
8
7
  extend ActiveSupport::Concern
9
8
 
@@ -2,6 +2,6 @@ module Platforms
2
2
  module Core
3
3
 
4
4
  # Version for display. Update as required.
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: platforms-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Elias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-10 00:00:00.000000000 Z
11
+ date: 2020-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -100,6 +100,20 @@ dependencies:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: generator_spec
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
103
117
  - !ruby/object:Gem::Dependency
104
118
  name: hashie
105
119
  requirement: !ruby/object:Gem::Requirement
@@ -208,7 +222,17 @@ files:
208
222
  - db/migrate/20200308091132_create_platforms_groups.rb
209
223
  - db/migrate/20200308091141_create_platforms_group_members.rb
210
224
  - db/migrate/20200313102128_create_platforms_certificates.rb
225
+ - lib/generators/platforms/core/base.rb
226
+ - lib/generators/platforms/core/install/USAGE
227
+ - lib/generators/platforms/core/install/install_generator.rb
228
+ - lib/generators/platforms/core/install/templates/platforms_core.rb
229
+ - lib/generators/platforms/core/network/USAGE
230
+ - lib/generators/platforms/core/network/network_generator.rb
231
+ - lib/generators/platforms/core/user/USAGE
232
+ - lib/generators/platforms/core/user/user_generator.rb
211
233
  - lib/platforms/core.rb
234
+ - lib/platforms/core/app_network.rb
235
+ - lib/platforms/core/app_user.rb
212
236
  - lib/platforms/core/configuration.rb
213
237
  - lib/platforms/core/engine.rb
214
238
  - lib/platforms/core/o_auth_2.rb