Class: RailsBase::Features::Chat

Inherits:
Object
  • Object
show all
Extended by:
CliActions, RailsBase::FileManipulation, RailsActions
Defined in:
lib/rs-rails-base/features/chat.rb

Constant Summary collapse

TEMPLATES_PATH =
'../templates/chat/'.freeze
VIEWS_PATH =
'app/views/api/v1/'.freeze

Class Method Summary collapse

Methods included from RailsBase::FileManipulation

append_to_file, create_file, inject_into_file, install_gem, read_all_content, replace_in_file

Methods included from CliActions

ask_for_something, say_something

Methods included from RailsActions

new_migration, run_migrations

Class Method Details

.create_files(templates_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rs-rails-base/features/chat.rb', line 31

def self.create_files(templates_path)
  create_file('app/channels/application_cable/channel.rb',
              "#{templates_path}channel.rb")
  create_file('app/channels/application_cable/connection.rb',
              "#{templates_path}connection.rb")
  create_file('app/services/chat_service.rb',
              "#{templates_path}chat_service.rb")
  create_file('app/channels/chat_channel.rb',
              "#{templates_path}chat_channel.rb")
  create_file('app/controllers/api/v1/chats_controller.rb',
              "#{templates_path}/chats_controller.rb")
  create_file('app/controllers/api/v1/messages_controller.rb',
              "#{templates_path}/messages_controller.rb")
end

.create_jbuilder_files(templates_path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rs-rails-base/features/chat.rb', line 58

def self.create_jbuilder_files(templates_path)
  create_file("#{VIEWS_PATH}chats/index.json.jbuilder",
              "#{templates_path}/index.json.jbuilder")
  create_file("#{VIEWS_PATH}chats/show.json.jbuilder",
              "#{templates_path}/show.json.jbuilder")
  create_file("#{VIEWS_PATH}chats/_info.json.jbuilder",
              "#{templates_path}/_info.json.jbuilder")
  create_file("#{VIEWS_PATH}chats/_message.json.jbuilder",
              "#{templates_path}/_message.json.jbuilder")
  create_file("#{VIEWS_PATH}messages/_info.json.jbuilder",
              "#{templates_path}/messages_create_info.json.jbuilder")
  create_file("#{VIEWS_PATH}messages/create.json.jbuilder",
              "#{templates_path}/messages_create.json.jbuilder")
end

.create_spec_files(templates_path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rs-rails-base/features/chat.rb', line 46

def self.create_spec_files(templates_path)
  create_file('spec/models/chat.rb', "#{templates_path}chat_spec.rb")
  create_file('spec/models/participant.rb', "#{templates_path}participant_spec.rb")
  create_file('spec/models/message.rb', "#{templates_path}message_spec.rb")
  create_file('spec/services/chat_service_spec.rb', "#{templates_path}chat_service_spec.rb")
  create_file('spec/factories/chat.rb', "#{templates_path}chat_factory.rb")
  create_file('spec/factories/participant.rb', "#{templates_path}participant_factory.rb")
  create_file('spec/factories/message.rb', "#{templates_path}message_factory.rb")
  create_file('spec/channels/chat_channel_spec.rb', "#{templates_path}chat_channel_spec.rb")
  create_file('spec/channels/connection_spec.rb', "#{templates_path}connection_spec.rb")
end

.inject_into_files(templates_path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rs-rails-base/features/chat.rb', line 73

def self.inject_into_files(templates_path)
  inject_into_file('config/routes.rb', ":profile\n        end", "#{templates_path}routes.rb")
  inject_into_file('config/routes.rb', "    end\n  end", "#{templates_path}routes_2.rb")
  inject_into_file('app/models/user.rb', "include DeviseTokenAuth::Concerns::User\n",
                   "#{templates_path}migration-models/user.rb")
  inject_into_file('app/controllers/api/v1/sessions_controller.rb',
                   "include Api::Concerns::ActAsApiRequest\n",
                   "#{templates_path}sessions_controller.rb", true)
  inject_into_file('spec/rails_helper.rb', "require 'simplecov'",
                   "#{templates_path}specs/rails_helper.rb")
end

.installObject



11
12
13
14
15
16
17
18
19
# File 'lib/rs-rails-base/features/chat.rb', line 11

def self.install
  install_gem('action-cable-testing', '0.3.1')
  migrations("#{TEMPLATES_PATH}migration-models/")
  create_files(TEMPLATES_PATH)
  create_jbuilder_files("#{TEMPLATES_PATH}jbuilder/")
  create_spec_files("#{TEMPLATES_PATH}specs/")
  inject_into_files(TEMPLATES_PATH)
  replace_in_files
end

.migrations(templates_path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/rs-rails-base/features/chat.rb', line 21

def self.migrations(templates_path)
  new_migration(templates_path, 'add_chat.rb')
  new_migration(templates_path, 'add_participant.rb')
  new_migration(templates_path, 'add_message.rb')
  run_migrations
  create_file('app/models/chat.rb', "#{templates_path}chat.rb")
  create_file('app/models/message.rb', "#{templates_path}message.rb")
  create_file('app/models/participant.rb', "#{templates_path}participant.rb")
end

.replace_in_filesObject



85
86
87
88
# File 'lib/rs-rails-base/features/chat.rb', line 85

def self.replace_in_files
  replace_in_file('config/cable.yml', "test:\n  adapter: async",
                  "test:\n  adapter: test")
end