Class: Notifykit::InstallGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



79
80
81
# File 'lib/generators/notifykit/install_generator.rb', line 79

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

.source_rootObject



12
13
14
# File 'lib/generators/notifykit/install_generator.rb', line 12

def self.source_root
  @source_root ||= File.join(File.dirname(__FILE__), 'templates')
end

Instance Method Details

#generate_notifykitObject



16
17
18
19
20
21
22
23
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/notifykit/install_generator.rb', line 16

def generate_notifykit
  generate_migration("create_notifications")

  # Ensure the destination structure
  empty_directory "app"
  empty_directory "app/models"
  empty_directory "app/mailers"
  empty_directory "app/helpers"
  empty_directory "app/controllers"
  empty_directory "app/views"
  empty_directory "app/views/notifications_mailer"
  empty_directory "spec"
  empty_directory "spec/models"
  empty_directory "spec/controllers"
  empty_directory "spec/mailers"

  # Fill out some templates (for now, this is just straight copy)
  template "app/models/notification.rb", "app/models/notification.rb"
  template "app/mailers/notifications_mailer.rb", "app/mailers/notifications_mailer.rb"
  template "app/helpers/notifications_helper.rb", "app/helpers/notifications_helper.rb"
  template "app/controllers/notifications_controller.rb", "app/controllers/notifications_controller.rb"
  template "spec/factories/notification.rb", "spec/factories/notification.rb"
  template "spec/models/notification_spec.rb", "spec/models/notification_spec.rb"
  template "spec/helpers/notifications_helper_spec.rb", "spec/helpers/notifications_helper_spec.rb"
  template "spec/mailers/notifications_mailer_spec.rb", "spec/mailers/notifications_mailer_spec.rb"
  template "spec/controllers/notifications_controller_spec.rb", "spec/controllers/notifications_controller_spec.rb"

  # Don't treat these like templates
  copy_file "app/views/notifications_mailer/notify.html.erb", "app/views/notifications_mailer/notify.html.erb"
  copy_file "app/views/notifications_mailer/notify.text.erb", "app/views/notifications_mailer/notify.text.erb"
  copy_file "app/views/notifications_mailer/_welcome.html.erb", "app/views/notifications_mailer/_welcome.html.erb"
  copy_file "app/views/notifications_mailer/_welcome.text.erb", "app/views/notifications_mailer/_welcome.text.erb"

  # Though many of these actions are not idempotent, you must be able to click them in an email
  route "get   '/notifications/recent', to: 'notifications#recent', as: :notifications_recent"
  route "get   '/notifications/:token', to: 'notifications#click', as: :notification_click"
  route "get   '/notifications/:token/view', to: 'notifications#view', as: :notification_view"
  route "get   '/notifications/:token/read', to: 'notifications#read', as: :notification_read"
  route "get   '/notifications/:token/ignore', to: 'notifications#ignore', as: :notification_ignore"
  route "get   '/notifications/:token/cancel', to: 'notifications#cancel', as: :notification_cancel"
  route "get   '/notifications/:token/unsubscribe', to: 'notifications#unsubscribe', as: :notification_unsubscribe"

  if options.test_mode?
    route "root   'welcome#index'"
    route "get    '/help', to: 'help#index', as: :help"
    route "get    '/privacy', to: 'privacy#index', as: :privacy"
    route "get    '/terms', to: 'terms#index', as: :terms"
  end

  # Adjust the user, unless it doesn't exist
  inject_into_class "app/models/user.rb", User, "has_many :notifications\n" rescue nil

  # Technically, we aren't inserting this at the end of the class, but the end of the RSpec::Configure
  insert_at_end_of_class "spec/spec_helper.rb", "spec/spec_helper.rb"

  # RSpec needs to be in the development group to be used in generators
  gem_group :test, :development do
    gem "rspec-rails"
    gem "shoulda-matchers"
    gem 'factory_girl_rails'
  end
end