Class: Talkable::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
SharedGeneratorMethods
Defined in:
lib/talkable/generators/install_generator.rb

Instance Method Summary collapse

Methods included from SharedGeneratorMethods

#erb?, #template_lang

Instance Method Details

#add_initializerObject



20
21
22
# File 'lib/talkable/generators/install_generator.rb', line 20

def add_initializer
  template "config/initializers/talkable.rb", "config/initializers/talkable.rb"
end

#ask_config_valuesObject



11
12
13
14
15
16
17
18
# File 'lib/talkable/generators/install_generator.rb', line 11

def ask_config_values
  puts "If you don't have Talkable account yet, create one here: \e]8;;https://admin.talkable.com/register?self_serve\ahttps://admin.talkable.com/register?self_serve\e]8;;\a"
  @site_slug  = ask("Your Talkable site slug:")
  @api_key    = ask("Your Talkable API Key:")
  if yes?('Do you have a custom domain? [Y/n]')
    @server   = ask("Your custom domain [#{Talkable::Configuration::DEFAULT_SERVER}]:")
  end
end

#inject_talkable_offerObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/talkable/generators/install_generator.rb', line 24

def inject_talkable_offer
  inject_into_file "app/controllers/application_controller.rb", after: "class ApplicationController < ActionController::Base" do
<<-RUBY

  before_action :load_talkable_offer
RUBY
  end

  inject_into_file "app/controllers/application_controller.rb", before: /^end/ do
<<-RUBY
  protected

  def load_talkable_offer
origin = Talkable.register_affiliate_member
@offer ||= origin.offer if origin
  end
RUBY
  end

  empty_directory "app/views/shared/"

  if erb?
    copy_file "app/views/shared/_talkable_offer.html.erb", "app/views/shared/_talkable_offer.html.erb"
    inject_into_file "app/views/layouts/application.html.erb", before: "</body>" do
      "<%= render 'shared/talkable_offer', offer: @offer %>\n"
    end
  else
    ext = template_lang

    copy_file "app/views/shared/_talkable_offer.html.#{ext}", "app/views/shared/_talkable_offer.html.#{ext}"
    gsub_file "app/views/layouts/application.html.#{ext}", /^(\s*)\=\s*yield\s*$/ do |line|
      paddings = line.match(/(\s*)\=/)[1]
      "#{line}#{paddings}= render 'shared/talkable_offer', offer: @offer\n"
    end
  end
end