Class: Funicular::Generators::ChatGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/funicular/chat/chat_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



13
14
15
# File 'lib/generators/funicular/chat/chat_generator.rb', line 13

def self.next_migration_number(dirname)
  ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#add_picoruby_include_tagObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/generators/funicular/chat/chat_generator.rb', line 70

def add_picoruby_include_tag
  layout = Rails.root.join("app", "views", "layouts", "application.html.erb")
  return unless File.exist?(layout)

  content = File.read(layout)
  return if content.include?("picoruby_include_tag")

  if content.include?("<%= csrf_meta_tags %>")
    inject_into_file layout.to_s,
                     "    <%= picoruby_include_tag %>\n",
                     after: "    <%= csrf_meta_tags %>\n"
  else
    say_status :skip, "layout does not contain csrf_meta_tags; add <%= picoruby_include_tag %> manually", :yellow
  end
end

#add_routesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/funicular/chat/chat_generator.rb', line 56

def add_routes
  routes_file = Rails.root.join("config", "routes.rb")
  if File.exist?(routes_file) && File.read(routes_file).include?('get "funicular_chat"')
    say_status :skip, "funicular_chat routes already exist", :yellow
    return
  end

  route <<~ROUTES
    get "funicular_chat", to: "funicular_chat#show"
    get "funicular_chat/messages", to: "funicular_chat_messages#index"
    post "funicular_chat/messages", to: "funicular_chat_messages#create"
  ROUTES
end

#create_funicular_filesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/funicular/chat/chat_generator.rb', line 42

def create_funicular_files
  template "funicular_chat_component.rb.tt",
           "app/funicular/components/funicular_chat_component.rb"
  template "funicular_chat_component_picotest.rb.tt",
           "test/funicular/client/funicular_chat_component_picotest.rb"

  @initializer_existed = File.exist?(Rails.root.join("app", "funicular", "initializer.rb"))
  if @initializer_existed
    say_status :skip, "app/funicular/initializer.rb already exists", :yellow
  else
    template "initializer.rb.tt", "app/funicular/initializer.rb"
  end
end

#create_rails_filesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/funicular/chat/chat_generator.rb', line 21

def create_rails_files
  migration_template "create_funicular_chat_messages.rb.tt",
                     "db/migrate/create_funicular_chat_messages.rb"
  template "funicular_chat_message.rb.tt",
           "app/models/funicular_chat_message.rb"
  template "funicular_chat_controller.rb.tt",
           "app/controllers/funicular_chat_controller.rb"
  template "funicular_chat_messages_controller.rb.tt",
           "app/controllers/funicular_chat_messages_controller.rb"
  template "application_cable_connection.rb.tt",
           "app/channels/application_cable/connection.rb" unless File.exist?(Rails.root.join("app", "channels", "application_cable", "connection.rb"))
  template "application_cable_channel.rb.tt",
           "app/channels/application_cable/channel.rb" unless File.exist?(Rails.root.join("app", "channels", "application_cable", "channel.rb"))
  template "funicular_chat_channel.rb.tt",
           "app/channels/funicular_chat_channel.rb"
  template "show.html.erb.tt",
           "app/views/funicular_chat/show.html.erb"
  template "funicular_chat.css.tt",
           "app/assets/stylesheets/funicular_chat.css"
end

#migration_versionObject



17
18
19
# File 'lib/generators/funicular/chat/chat_generator.rb', line 17

def migration_version
  "[#{ActiveRecord::Migration.current_version}]"
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/generators/funicular/chat/chat_generator.rb', line 86

def print_next_steps
  say ""
  say "Funicular chat generated.", :green
  say ""
  say "Next steps:"
  say "  1. Run `bin/rails db:migrate`"
  say "  2. Run `bin/rails funicular:compile`"
  say "  3. Open `/funicular_chat` in two browser windows"
  say ""

  if @initializer_existed
    say "Add this route inside your existing Funicular.start block:"
    say "  router.get('/funicular_chat', to: FunicularChatComponent, as: 'funicular_chat')"
    say ""
  end
end