Class: ArSync::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/ar_sync/install/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_api_controllerObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/ar_sync/install/install_generator.rb', line 31

def create_api_controller
  create_file 'app/controllers/sync_api_controller.rb', <<~CODE
    class SyncApiController < ApplicationController
      include ArSync::ApiControllerConcern
      def schema
        SyncSchema.new
      end
    end
  CODE
end

#create_configObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/ar_sync/install/install_generator.rb', line 42

def create_config
  create_file 'config/initializers/ar_sync.rb', <<~CODE
    ActiveRecord::Base.include ArSync::ModelBase
    ArSync.configure do |config|
      config.current_user_method = :current_user
      config.key_prefix = 'ar_sync_'
      config.key_secret = '#{SecureRandom.hex}'
      config.key_expires_in = 30.seconds
    end
  CODE
end

#create_schema_classObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/generators/ar_sync/install/install_generator.rb', line 3

def create_schema_class
  create_file 'app/models/sync_schema.rb', <<~CODE
    class SyncSchema < ArSync::SyncSchemaBase
      # serializer_field :profile, type: User do |current_user|
      #   current_user
      # end

      # serializer_field :post, type: Post do |current_user, id:|
      #   Post.where(current_user_can_access).find_by id: id
      # end

      # Reload API for all types should be defined here.

      # serializer_field :User do |current_user, ids:|
      #   User.where(current_user_can_access).where id: ids
      # end

      # serializer_field :Post do |current_user, ids:|
      #   Post.where(current_user_can_access).where id: ids
      # end

      # serializer_field :Comment do |current_user, ids:|
      #   Comment.where(current_user_can_access).where id: ids
      # end
    end
  CODE
end

#create_sync_channelObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/ar_sync/install/install_generator.rb', line 54

def create_sync_channel
  create_file 'app/channels/sync_channel.rb', <<~CODE
    class SyncChannel < ApplicationCable::Channel
      def subscribed
        key = ArSync.validate_expiration params[:key]
        stream_from key if key
      end
    end
  CODE
end

#setup_jsObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/ar_sync/install/install_generator.rb', line 75

def setup_js
  inject_into_file(
    'app/assets/javascripts/application.js',
    [
      '//= require ar_sync',
      '//= require action_cable',
      '//= require ar_sync_action_cable_adapter',
      'ArSyncModel.setConnectionAdapter(new ArSyncActionCableAdapter(ActionCable))'
    ].join("\n") + "\n",
    before: '//= require_tree .'
  )
end

#setup_routesObject



65
66
67
68
69
70
71
72
73
# File 'lib/generators/ar_sync/install/install_generator.rb', line 65

def setup_routes
  inject_into_file(
    'config/routes.rb',
    "\n  post '/sync_api', to: 'sync_api#sync_call'" +
    "\n  post '/static_api', to: 'sync_api#static_call'" +
    "\n  post '/graphql', to: 'sync_api#graphql_call'",
    after: 'Rails.application.routes.draw do'
  )
end