forced 0.2.0 → 1.0.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
  SHA1:
3
- metadata.gz: 24410ac9c0767e607034525d8168837dc87231bf
4
- data.tar.gz: 1706c0b98edca3bc8b4e09779c829fdc74c07180
3
+ metadata.gz: 0f8bfe65c4245d7193e813ab666edea94a8d9bc4
4
+ data.tar.gz: 95c773c3051dee62f912d14cf00e6e6ca7628aa5
5
5
  SHA512:
6
- metadata.gz: 16f9039bfcd3f53f2a91148d28f3c52f8363e2c735eca9e3d76ff9d2c4ca7ae5d2495313e6280d5e7eeb91ba39eb7f03525ba516158daecba4a9e2d723e25a49
7
- data.tar.gz: 49b535a85af7581037e7db44d8dceaba655db7454e17a38f70c1f074cd0c7d61161d5a422f7540d78648dad963ba260f35411b68bc74cc125c84e4b75024fa10
6
+ metadata.gz: 1f10f544891d22c7b6c9f3eded67de2f93a87a0de60f87a9a908f39d5da7f5aade90af1a51e46f302b1e7777db21f9386d089018b2f2bfc252f75ce80b7dbcae
7
+ data.tar.gz: ef0fafa3cb75e9815fbc5291e118ff9254270f5b2cd038f7094c0df6a0a59c0827c90adfccde9ddf55e592a44a7bc488252304a400dead669c88a0f7d827530c
data/MIT-LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -38,14 +38,40 @@ After all these are done, add the line below to your routes file.
38
38
  mount Forced::Engine => "/forced"
39
39
  ```
40
40
 
41
- You are all set!
41
+ You endpoint and tables are set!
42
42
 
43
43
  ## Usage
44
44
 
45
- Module needs to get the coming request to prepare the response. As long as request headers contains `X-Platform` and `X-Client-Version`, you are good to go.
45
+ Add `is_versionable` to any model you want to keep as a parent for `Forced::Client` model.
46
46
 
47
+ For example, imagine you have a `Brand` model to identify each application. Adding the line below to your model, will hold a relation for `has_many :clients'
47
48
 
48
- Then send a `GET` request to `{{url}}/forced/status`. This will return the below JSON.
49
+ ```ruby
50
+ class Brand < ApplicationRecord
51
+ # ...
52
+
53
+ is_versionable
54
+
55
+ # ...
56
+ end
57
+ ```
58
+
59
+ Then, you can call your clients with;
60
+
61
+ ```ruby
62
+ Brand.find(:id).clients
63
+ # => #<ActiveRecord::Associations::CollectionProxy [...]>
64
+
65
+ Brand.find(:id).clients.to_sql
66
+ # => "SELECT \"forced_clients\".* FROM \"forced_clients\" WHERE \"forced_clients\".\"item_id\" = :id AND \"forced_clients\".\"item_type\" = 'Brand'"
67
+ ```
68
+
69
+ The Forced module needs to get the coming request to prepare the response. As long as request headers contains `X-Platform` and `X-Client-Version`, you are good to go.
70
+
71
+ * `X-Platform` will need to match with `Forced::Client identifier:string` attribute in order to search the available versions.
72
+ * `X-Client-Version` should contain a semantic version number, e.g. `1.0.0`
73
+
74
+ Then send a `GET` request to `{{url}}/forced/status`. This will return the below JSON. (`/forced` is where you mounted the engine!)
49
75
 
50
76
  ```json
51
77
  {
@@ -61,25 +87,25 @@ If you want to return some version of this hash, you can access the response by
61
87
  response = Forced::Response.call(request)
62
88
  ```
63
89
 
64
- Client enum is `[:android, :ios]` at default. To change it, open up an initializer for `Forced` module and change the constant named `CLIENT_ENUM`.
65
-
66
- ```ruby
67
- module Forced
68
- CLIENT_ENUM = [:android, :ios]
69
- end
70
- ```
71
-
72
- To create a record, you can use your Rails console.
90
+ To create a record, you can use your Rails console. `Forced::Client`
73
91
 
74
92
  ```ruby
75
- Forced::AppVersion.new
93
+ Forced::Client.new
94
+ # => #<Forced::Client id: nil, item_type: nil, item_id: nil, identifier: nil, deleted_at: nil, created_at: nil, updated_at: nil>
76
95
 
77
- # => #<Forced::AppVersion id: nil, client: nil, version: nil, force_update: false, changelog: nil, created_at: nil, updated_at: nil>
96
+ Forced::Version.new
97
+ # => #<Forced::Version id: nil, client_id: nil, version: nil, force_update: false, changelog: nil, deleted_at: nil, created_at: nil, updated_at: nil>
78
98
  ```
79
99
 
80
100
  ## Responses
81
101
 
82
102
  All available under `Forced::MESSAGES` hash table. You can override the values as you wish. Also checkout the `check_update_status` private method in `base.rb` to understand the cases.
83
103
 
104
+ ## Upgrading from 0.2.0 to 1.0.0
105
+
106
+ New migrations and tables have a different name, so, unless you are using custom calls, you can optionally and gradually create a migration for old table and move your records into the new table.
107
+
108
+ If you had some trouble or found something that wasn't suppose to be there, feel free to open an issue.
109
+
84
110
  ## License
85
111
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ module Forced
2
+ class Client < ApplicationRecord
3
+ belongs_to :item, polymorphic: true
4
+
5
+ has_many :versions, class_name: 'Forced::Version', foreign_key: 'client_id'
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Forced
2
+ class Version < ApplicationRecord
3
+ belongs_to :client, class_name: 'Forced::Client'
4
+
5
+ validates :version, presence: true, length: { maximum: 255 }
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Forced
2
+ def self.table_name_prefix
3
+ 'forced_'
4
+ end
5
+ end
data/config/routes.rb CHANGED
File without changes
data/lib/forced/base.rb CHANGED
@@ -5,7 +5,7 @@ module Forced
5
5
  @client_platform = get_client_platform(request)
6
6
  @client_version = get_client_version(request)
7
7
 
8
- @client_version_records = (@client_platform && @client_version ? Forced::AppVersion.where(client: @client_platform) : nil)
8
+ @client_version_records = (@client_platform && @client_version ? Forced::Client.find_by!(identifier: @client_platform).versions : nil)
9
9
  @versions_after_client = @client_version_records&.where('version > ?', @client_version)
10
10
  @latest_app_version = @client_version_records&.last
11
11
  @any_forced_in_the_future = @versions_after_client&.pluck(:force_update)&.any?
data/lib/forced/engine.rb CHANGED
File without changes
@@ -0,0 +1,15 @@
1
+ require 'forced/versionable'
2
+
3
+ module Forced
4
+ module Model
5
+ def self.included(base)
6
+ base.send :extend, ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def is_versionable
11
+ send :include, Forced::Versionable
12
+ end
13
+ end
14
+ end
15
+ end
File without changes
File without changes
@@ -1,6 +1,6 @@
1
1
  module Forced
2
- MAJOR = 0
3
- MINOR = 2
2
+ MAJOR = 1
3
+ MINOR = 0
4
4
  TINY = 0
5
5
  PRE = nil
6
6
 
@@ -0,0 +1,9 @@
1
+ module Forced
2
+ module Versionable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :clients, class_name: 'Forced::Client', as: :item
7
+ end
8
+ end
9
+ end
data/lib/forced.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'forced/engine'
2
2
  require 'forced/base'
3
3
  require 'forced/response'
4
- require 'forced/client_enum'
5
4
  require 'forced/messages'
5
+ require 'forced/is_versionable'
6
6
 
7
7
  module Forced
8
+ ActiveSupport.on_load(:active_record) do
9
+ include Forced::Model
10
+ end
8
11
  end
@@ -14,10 +14,10 @@ module Forced
14
14
 
15
15
  source_root File.expand_path('templates', __dir__)
16
16
 
17
- desc "Generates (but does not run) a migration to add a forced_app_versions table."
17
+ desc "Generates (but does not run) a migration to add a forced_app_clients and forced_app_versions table."
18
18
 
19
19
  def create_migration_file
20
- add_paper_trail_migration('create_forced_app_versions')
20
+ add_forced_app_migration('create_forced_tables')
21
21
  end
22
22
 
23
23
  def self.next_migration_number(dirname)
@@ -26,7 +26,7 @@ module Forced
26
26
 
27
27
  protected
28
28
 
29
- def add_paper_trail_migration(template)
29
+ def add_forced_app_migration(template)
30
30
  migration_dir = File.expand_path('db/migrate')
31
31
 
32
32
  if self.class.migration_exists?(migration_dir, template)
@@ -36,7 +36,7 @@ module Forced
36
36
  "#{template}.rb.erb",
37
37
  "db/migrate/#{template}.rb",
38
38
  migration_version: migration_version,
39
- forced_app_versions_table_options: forced_app_versions_table_options
39
+ forced_table_options: forced_table_options
40
40
  )
41
41
  end
42
42
  end
@@ -54,7 +54,7 @@ module Forced
54
54
  MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
55
55
  end
56
56
 
57
- def forced_app_versions_table_options
57
+ def forced_table_options
58
58
  if mysql?
59
59
  ', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
60
60
  else
@@ -62,4 +62,4 @@ module Forced
62
62
  end
63
63
  end
64
64
  end
65
- end
65
+ end
@@ -0,0 +1,24 @@
1
+ # This migration creates the `app_clients` and `app_versions` table,
2
+ # which `forced` gem requires to run properly.
3
+
4
+ class CreateForcedTables < ActiveRecord::Migration<%= migration_version %>
5
+ def change
6
+ create_table :forced_clients<%= forced_table_options %> do |t|
7
+ t.references :item, polymorphic: true, index: true
8
+ t.string :identifier, index: { unique: true }
9
+ t.datetime :deleted_at
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ create_table :forced_versions<%= forced_table_options %> do |t|
15
+ t.references :client, foreign_key: { to_table: :forced_clients }
16
+ t.string :version, limit: 255
17
+ t.boolean :force_update, nil: false, default: false
18
+ t.text :changelog
19
+ t.datetime :deleted_at
20
+
21
+ t.timestamps
22
+ end
23
+ end
24
+ end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aoozdemir
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-13 00:00:00.000000000 Z
11
+ date: 2018-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -64,18 +64,21 @@ files:
64
64
  - Rakefile
65
65
  - app/controllers/forced/application_controller.rb
66
66
  - app/controllers/forced/status_controller.rb
67
- - app/models/forced/app_version.rb
67
+ - app/models/forced.rb
68
68
  - app/models/forced/application_record.rb
69
+ - app/models/forced/client.rb
70
+ - app/models/forced/version.rb
69
71
  - config/routes.rb
70
72
  - lib/forced.rb
71
73
  - lib/forced/base.rb
72
- - lib/forced/client_enum.rb
73
74
  - lib/forced/engine.rb
75
+ - lib/forced/is_versionable.rb
74
76
  - lib/forced/messages.rb
75
77
  - lib/forced/response.rb
76
78
  - lib/forced/version.rb
79
+ - lib/forced/versionable.rb
77
80
  - lib/generators/forced/install_generator.rb
78
- - lib/generators/forced/templates/create_forced_app_versions.rb.erb
81
+ - lib/generators/forced/templates/create_forced_tables.rb.erb
79
82
  - lib/tasks/forced_tasks.rake
80
83
  homepage: https://github.com/aoozdemir/forced
81
84
  licenses:
@@ -1,21 +0,0 @@
1
- # == Schema Information
2
- #
3
- # Table name: forced_app_versions
4
- #
5
- # id :integer not null, primary key
6
- # client :integer
7
- # version :string(255)
8
- # force_update :boolean default(FALSE)
9
- # changelog :text
10
- # created_at :datetime not null
11
- # updated_at :datetime not null
12
- #
13
-
14
- module Forced
15
- class AppVersion < ApplicationRecord
16
- enum client: Forced::CLIENT_ENUM
17
-
18
- validates :client, presence: true
19
- validates :version, presence: true, length: { maximum: 255 }
20
- end
21
- end
@@ -1,3 +0,0 @@
1
- module Forced
2
- CLIENT_ENUM = [:android, :ios]
3
- end
@@ -1,15 +0,0 @@
1
- # This migration creates the `app_versions` table, the only schema PT requires.
2
- # All other migrations PT provides are optional.
3
- class CreateForcedAppVersions < ActiveRecord::Migration<%= migration_version %>
4
-
5
- def change
6
- create_table :forced_app_versions<%= forced_app_versions_table_options %> do |t|
7
- t.integer :client
8
- t.string :version, limit: 255
9
- t.boolean :force_update, nil: false, default: false
10
- t.text :changelog
11
-
12
- t.timestamps
13
- end
14
- end
15
- end