Skip to content

Commit

Permalink
Add tests for generators
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Aug 16, 2016
1 parent 5ac949c commit 601a607
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 38 deletions.
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
ammeter (1.1.3)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-rails (>= 2.2)
arel (6.0.3)
bcrypt (3.1.11)
builder (3.2.2)
Expand Down Expand Up @@ -166,6 +170,7 @@ PLATFORMS

DEPENDENCIES
activity_notification!
ammeter (~> 1.1.3)
coveralls
devise (~> 4.2.0)
factory_girl_rails (~> 4.7.0)
Expand Down
1 change: 1 addition & 0 deletions activity_notification.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-rails", '~> 3.5.1'
s.add_development_dependency "factory_girl_rails", '~> 4.7.0'
s.add_development_dependency 'simplecov', '~> 0.12.0'
s.add_development_dependency 'ammeter', '~> 1.1.3'
s.add_development_dependency "devise", '~> 4.2.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module Generators
class MigrationGenerator < ActiveRecord::Generators::Base
source_root File.expand_path("../../../templates/active_record", __FILE__)

argument :name, type: :string, default: 'create_notifications'
argument :name, type: :string, default: 'CreateNotifications'

# Create migration in project's folder
def generate_files
migration_template 'migration.rb', "db/migrate/#{name}.rb"
# Create migration files in application directory
def create_migrations
@migration_name = name
migration_template 'migration.rb', "db/migrate/#{name.underscore}.rb"
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActivityNotification
module Generators
class ControllersGenerator < Rails::Generators::Base
CONTROLLERS = %w(notifications notifications_with_devise).freeze
CONTROLLERS = ['notifications', 'notifications_with_devise'].freeze

desc <<-DESC.strip_heredoc
Create inherited ActivityNotification controllers in your app/controllers folder.
Expand Down
7 changes: 2 additions & 5 deletions lib/generators/activity_notification/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module Generators

class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates", __FILE__)
``

desc "Creates a ActivityNotification initializer and copy locale files to your application."
class_option :orm

def copy_initializer

#TODO suport other orm e.g. mongoid
unless options[:orm] == :active_record
raise MissingORMError, <<-ERROR.strip_heredoc
raise TypeError, <<-ERROR.strip_heredoc
Currently ActivityNotification is only supported with Active Record ORM.
Be sure to have an Active Record ORM loaded in your
Expand All @@ -37,9 +37,6 @@ def show_readme
readme "README" if behavior == :invoke
end

def rails_4?
Rails::VERSION::MAJOR == 4
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module Generators
class NotificationGenerator < ActiveRecord::Generators::Base
source_root File.expand_path("../../../templates/notification", __FILE__)

argument :name, type: :string, default: 'notification'
argument :name, type: :string, default: 'Notification'

# Create model in project's folder
def generate_files
copy_file 'notification.rb', "app/models/#{name}.rb"
# Create model in application directory
def create_models
@model_name = name
template 'notification.rb', "app/models/#{name.underscore}.rb"
end
end
end
Expand Down
42 changes: 20 additions & 22 deletions lib/generators/activity_notification/views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,39 @@
module ActivityNotification
module Generators
# Include this module in your generator to generate ActivityNotification views.
# `copy_views` is the main method and by default copies all views
# with forms.
# `copy_views` is the main method and by default copies all views of ActivityNotification.
class ViewsGenerator < Rails::Generators::Base
VIEWS = [:notifications, :mailer].freeze

source_root File.expand_path("../../../../app/views/activity_notification", __FILE__)
desc "Copies default ActivityNotification views to your application."

argument :target, required: false, default: nil,
desc: "The target to copy views to"
class_option :views, aliases: "-v", type: :array, desc: "Select specific view directories to generate (notifications, mailer)"
desc: "The target to copy views to"
class_option :views, aliases: "-v", type: :array,
desc: "Select specific view directories to generate (notifications, mailer)"
public_task :copy_views

def copy_views
if options[:views]
options[:views].each do |directory|
view_directory directory.to_sym
end
else
view_directory :notifications
view_directory :mailer
target_views = options[:views] || VIEWS
target_views.each do |directory|
view_directory directory.to_sym
end
end

protected

def view_directory(name, _target_path = nil)
directory "#{name.to_s}/default", _target_path || "#{target_path}/#{name}/#{plural_target || :default}"
end

def target_path
@target_path ||= "app/views/activity_notification"
end

def plural_target
@plural_target ||= target.presence && target.to_s.underscore.pluralize
end
def view_directory(name, _target_path = nil)
directory "#{name.to_s}/default", _target_path || "#{target_path}/#{name}/#{plural_target || :default}"
end
def target_path
@target_path ||= "app/views/activity_notification"
end
def plural_target
@plural_target ||= target.presence && target.to_s.underscore.pluralize
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/templates/active_record/migration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Migration responsible for creating a table with notifications
class CreateNotifications < ActiveRecord::Migration
class <%= @migration_name %> < ActiveRecord::Migration
# Create table
def change
create_table :notifications do |t|
Expand Down
5 changes: 4 additions & 1 deletion lib/generators/templates/notification/notification.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Notification model for customisation & custom methods
class Notification < ActivityNotification::Notification
class <%= @model_name %> < ActivityNotification::Notification

# Write custom methods or override methods here

end
41 changes: 41 additions & 0 deletions spec/generators/active_record/migration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'generators/activity_notification/active_record/migration_generator'

describe ActivityNotification::Generators::MigrationGenerator, type: :generator do

# setup_default_destination
destination File.expand_path("../../../../tmp", __FILE__)
before { prepare_destination }

it 'runs generating migration tasks' do
gen = generator
expect(gen).to receive :create_migrations
gen.invoke_all
end

describe 'the generated files' do
context 'without name argument' do
before do
run_generator
end

describe 'CreateNotifications migration file' do
subject { file(Dir["tmp/db/migrate/*_create_notifications.rb"].first.gsub!('tmp/', '')) }
it { is_expected.to exist }
it { is_expected.to contain(/class CreateNotifications < ActiveRecord::Migration/) }
end
end

context 'with CreateCustomNotifications as name argument' do
before do
run_generator %w(CreateCustomNotifications)
end

describe 'CreateCustomNotifications migration file' do
subject { file(Dir["tmp/db/migrate/*_create_custom_notifications.rb"].first.gsub!('tmp/', '')) }
it { is_expected.to exist }
it { is_expected.to contain(/class CreateCustomNotifications < ActiveRecord::Migration/) }
end
end

end
end
62 changes: 62 additions & 0 deletions spec/generators/controllers_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'generators/activity_notification/controllers_generator'

describe ActivityNotification::Generators::ControllersGenerator, type: :generator do

# setup_default_destination
destination File.expand_path("../../../tmp", __FILE__)
before { prepare_destination }

it 'runs generating controllers tasks' do
gen = generator %w(users)
expect(gen).to receive :create_controllers
expect(gen).to receive(:readme).and_return(true)
gen.invoke_all
end

describe 'the generated files' do
context 'without target argument' do
it 'raise Thor::RequiredArgumentMissingError' do
expect { run_generator }
.to raise_error(Thor::RequiredArgumentMissingError)
end
end

context 'with users as target' do
context 'with target controllers as default' do
before do
run_generator %w(users)
end

describe 'the notifications_controller' do
subject { file('app/controllers/users/notifications_controller.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/class Users::NotificationsController < ActivityNotification::NotificationsController/) }
end

describe 'the notifications_with_devise_controller' do
subject { file('app/controllers/users/notifications_with_devise_controller.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/class Users::NotificationsWithDeviseController < ActivityNotification::NotificationsWithDeviseController/) }
end
end

context 'with a controllers option as notifications' do
before do
run_generator %w(users --controllers notifications)
end

describe 'the notifications_controller' do
subject { file('app/controllers/users/notifications_controller.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/class Users::NotificationsController < ActivityNotification::NotificationsController/) }
end

describe 'the notifications_with_devise_controller' do
subject { file('app/controllers/users/notifications_with_devise_controller.rb') }
it { is_expected.not_to exist }
end
end
end

end
end
43 changes: 43 additions & 0 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'generators/activity_notification/install_generator'

describe ActivityNotification::Generators::InstallGenerator, type: :generator do

# setup_default_destination
destination File.expand_path("../../../tmp", __FILE__)
before { prepare_destination }

it 'runs both the initializer and locale tasks' do
gen = generator
expect(gen).to receive :copy_initializer
expect(gen).to receive :copy_locale
expect(gen).to receive(:readme).and_return(true)
gen.invoke_all
end

describe 'the generated files' do
context 'with active_record orm as default' do
before do
run_generator
end

describe 'the initializer' do
subject { file('config/initializers/activity_notification.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/ActivityNotification.configure do |config|/) }
end

describe 'the locale file' do
subject { file('config/locales/activity_notification.en.yml') }
it { is_expected.to exist }
it { is_expected.to contain(/en:\n.+notification:\n.+default:/) }
end
end

context 'with orm option as not :active_record' do
it 'raise MissingORMError' do
expect { run_generator %w(--orm dummy) }
.to raise_error(TypeError)
end
end
end
end
41 changes: 41 additions & 0 deletions spec/generators/models/notification_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'generators/activity_notification/models/notification_generator'

describe ActivityNotification::Generators::NotificationGenerator, type: :generator do

# setup_default_destination
destination File.expand_path("../../../../tmp", __FILE__)
before { prepare_destination }

it 'runs generating model tasks' do
gen = generator
expect(gen).to receive :create_models
gen.invoke_all
end

describe 'the generated files' do
context 'without name argument' do
before do
run_generator
end

describe 'app/models/notification.rb' do
subject { file('app/models/notification.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/class Notification < ActivityNotification::Notification/) }
end
end

context 'with CustomNotification as name argument' do
before do
run_generator %w(CustomNotification)
end

describe 'app/models/notification.rb' do
subject { file('app/models/custom_notification.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/class CustomNotification < ActivityNotification::Notification/) }
end
end

end
end
Loading

0 comments on commit 601a607

Please sign in to comment.