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



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
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/ar_sync/install/install_generator.rb', line 5

def create_api_controller
  base_code = <<~CODE
    include ArSync::ApiControllerConcern

    # serializer_field :profile, type: User do |_user|
    #   current_user
    # end

    # serializer_field :post, type: Post do |_user, id:|
    #   Post.where(current_user_can_access).find id
    # end
  CODE
  graph_additional_code = <<~CODE
    # Reload API for all types should be defined here.

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

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

    # serializer_field :Comment do |_user, ids:|
    #   Comment.where(current_user_can_access).where id: ids
    # end
  CODE
  controller_body = options['mode'] == 'tree' ? base_code : base_code + "\n" + graph_additional_code
  code = [
    "class SyncApiController < ApplicationController\n",
    controller_body.lines.map { |l| l.blank? ? l : '  ' + l },
    "end\n"
  ].join
  create_file 'app/controllers/sync_api_controller.rb', code
end

#create_configObject



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

def create_config
  create_file 'config/initializers/ar_sync.rb', <<~CODE
    ArSync.use :#{options['mode']}
    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_sync_channelObject



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

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



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

def setup_js
  inject_into_file(
    'app/assets/javascripts/application.js',
    [
      "//= require ar_sync_#{options['mode']}",
      '//= require action_cable',
      '//= require ar_sync_actioncable_adapter',
      'ArSyncModel.setConnectionAdapter(new ArSyncActionCableAdapter())'
    ].join("\n") + "\n",
    before: '//= require_tree .'
  )
end

#setup_routesObject



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

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