Skip to content

Commit

Permalink
Added generators:
Browse files Browse the repository at this point in the history
	* devise_invitable:install
	* devise_invitable MODEL
	* devise_invitable:views (thanks to robotblake for this one)

See README for more info.

Added :slow => true for slow generators' specs
  • Loading branch information
Rémy Coutable committed Sep 2, 2010
1 parent 1d37456 commit 443fe59
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 21 deletions.
13 changes: 13 additions & 0 deletions lib/generators/active_record/devise_invitable_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rails/generators/active_record'

module ActiveRecord
module Generators
class DeviseInvitableGenerator < ActiveRecord::Generators::Base
source_root File.expand_path("../templates", __FILE__)

def copy_devise_migration
migration_template "migration.rb", "db/migrate/devise_invitable_add_to_#{table_name}"
end
end
end
end
18 changes: 18 additions & 0 deletions lib/generators/active_record/templates/migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
def self.up
change_table :<%= table_name %> do |t|
t.string :invitation_token, :limit => 20
t.datetime :invitation_sent_at
t.index :invitation_token # for invitable
end

# And allow null encrypted_password and password_salt:
change_column :<%= table_name %>, :encrypted_password, :string, :null => true
change_column :<%= table_name %>, :password_salt, :string, :null => true
end

def self.down
remove_column :<%= table_name %>, :invitation_sent_at
remove_column :<%= table_name %>, :invitation_token
end
end
16 changes: 16 additions & 0 deletions lib/generators/devise_invitable/devise_invitable_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module DeviseInvitable
module Generators
class DeviseInvitableGenerator < Rails::Generators::NamedBase
namespace "devise_invitable"

desc "Add :invitable directive in the given model. Also generate migration for ActiveRecord"

def inject_devise_invitable_content
path = File.join("app", "models", "#{file_path}.rb")
inject_into_file(path, "invitable, :", :after => "devise :") if File.exists?(path)
end

hook_for :orm
end
end
end
39 changes: 39 additions & 0 deletions lib/generators/devise_invitable/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module DeviseInvitable
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates", __FILE__)

desc "Add DeviseInvitable config variables to the Devise initializer and copy DeviseInvitable locale files to your application."

def add_config_options_to_initializer
devise_initializer_path = "config/initializers/devise.rb"
if File.exist?(devise_initializer_path)
old_content = File.read(devise_initializer_path)

if old_content.match(Regexp.new(/^\s# ==> Configuration for :invitable\n/))
false
else
inject_into_file(devise_initializer_path, :before => " # ==> Configuration for :confirmable\n") do
<<-CONTENT
# ==> Configuration for :invitable
# Time interval where the invitation token is valid (default: 0).
# If invite_for is 0 or nil, the invitation will never expire.
# config.invite_for = 2.weeks
# Flag that force a record to be valid before being actually invited
# (default: false).
# config.validate_on_invite = true
CONTENT
end
end
end
end

def copy_locale
copy_file "../../../config/locales/en.yml", "config/locales/devise_invitable.en.yml"
end

end
end
end
2 changes: 1 addition & 1 deletion lib/generators/devise_invitable/views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module DeviseInvitable
module Generators
class ViewsGenerator < Devise::Generators::ViewsGenerator
source_root File.expand_path('../../../../app/views', __FILE__)
source_root File.expand_path("../../../../app/views", __FILE__)
desc 'Copies all DeviseInvitable views to your application.'
end
end
Expand Down
101 changes: 101 additions & 0 deletions spec/devise_invitable/generators_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'spec_helper'
require 'rails/generators'
require 'generators/devise_invitable/devise_invitable_generator'

describe DeviseInvitable::Generators::DeviseInvitableGenerator, :slow => true do
RAILS_APP_PATH = File.expand_path("../../rails_app", __FILE__)

describe "rails g" do
before(:each) { @output = `cd #{RAILS_APP_PATH} && rails g` }

it "should include the 3 generators" do
@output.should include("DeviseInvitable:\n devise_invitable\n devise_invitable:install\n devise_invitable:views")
end
end

describe "rails g devise_invitable:install" do
before(:all) { @output = `cd #{RAILS_APP_PATH} && rails g devise_invitable:install -p` }

it "should include inject config/initializers/devise.rb" do
@output.should =~ %r(inject.+ config/initializers/devise\.rb\n)
end
it "should include create config/locales/devise_invitable.en.yml" do
@output.should =~ %r(create.+ config/locales/devise_invitable\.en\.yml\n)
end
end

describe "rails g devise_invitable:views" do
context "not scoped" do
before(:all) { @output = `cd #{RAILS_APP_PATH} && rails g devise_invitable:views -p` }

it "should include create app/views/devise" do
@output.should =~ %r(create.+ app/views/devise\n)
end
it "should include create app/views/devise/invitations/edit.html.erb" do
@output.should =~ %r(create.+ app/views/devise/invitations/edit\.html\.erb\n)
end
it "should include create app/views/devise/invitations/new.html.erb" do
@output.should =~ %r(create.+ app/views/devise/invitations/new\.html\.erb\n)
end
it "should include app/views/devise/mailer/invitation_instructions.html.erb" do
@output.should =~ %r(create.+ app/views/devise/mailer/invitation_instructions\.html\.erb\n)
end
end

context "scoped" do
before(:all) { @output = `cd #{RAILS_APP_PATH} && rails g devise_invitable:views octopussies -p` }

it "should include create app/views/octopussies" do
@output.should =~ %r(create.+ app/views/octopussies\n)
end
it "should include create app/views/octopussies/invitations/edit.html.erb" do
@output.should =~ %r(create.+ app/views/octopussies/invitations/edit\.html\.erb\n)
end
it "should include create app/views/octopussies/invitations/new.html.erb" do
@output.should =~ %r(create.+ app/views/octopussies/invitations/new\.html\.erb\n)
end
it "should include app/views/octopussies/mailer/invitation_instructions.html.erb" do
@output.should =~ %r(create.+ app/views/octopussies/mailer/invitation_instructions\.html\.erb\n)
end
end

pending "haml" do
before(:all) do
RailsApp::Application.config.generators.options[:rails][:template_engine] = :haml
@output = `cd #{RAILS_APP_PATH} && rails g devise_invitable:views octopussies -p`
puts RailsApp::Application.config.generators.options[:rails][:template_engine]
end

it "should include create app/views/octopussies" do
@output.should =~ %r(create.+ app/views/octopussies\n)
end
it "should include create app/views/octopussies/invitations/edit.html.haml" do
@output.should =~ %r(create.+ app/views/octopussies/invitations/edit\.html\.haml\n)
end
it "should include create app/views/octopussies/invitations/new.html.erb" do
@output.should =~ %r(create.+ app/views/octopussies/invitations/new\.html\.haml\n)
end
it "should include app/views/octopussies/mailer/invitation_instructions.html.erb" do
@output.should =~ %r(create.+ app/views/octopussies/mailer/invitation_instructions\.html\.haml\n)
end
end
end

describe "rails g devise_invitable Octopussy" do
before(:each) { @output = `cd #{RAILS_APP_PATH} && rails g devise_invitable Octopussy -p` }

it "should include inject app/models/octopussy.rb" do
@output.should =~ %r(inject.+ app/models/octopussy\.rb\n)
end
it "should include invoke active_record" do
@output.should =~ %r(invoke.+ #{DEVISE_ORM}\n)
end
it "should include create db/migrate/\d{14}_devise_invitable_add_to_octopussies.rb if orm is ActiveRecord" do
if DEVISE_ORM == :active_record
@output.should =~ %r(create.+ db/migrate/\d{14}_devise_invitable_add_to_octopussies\.rb\n)
elsif DEVISE_ORM == :mongoid
@output.should_not =~ %r(create.+ db/migrate/\d{14}_devise_invitable_add_to_octopussies\.rb\n)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/devise_invitable/mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require 'spec_helper'

describe Devise::Models::Invitable do
before(:each) do
Expand Down
2 changes: 1 addition & 1 deletion spec/devise_invitable/model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require 'spec_helper'

describe Devise::Models::Invitable do

Expand Down
2 changes: 1 addition & 1 deletion spec/devise_invitable/models_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require 'spec_helper'

class Invitable < User
devise :invitable, :invite_for => 2.weeks, :validate_on_invite => true
Expand Down
11 changes: 11 additions & 0 deletions spec/rails_app/app/models/octopussy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This model is here for the generators' specs
if DEVISE_ORM == :active_record
class Octopussy < ActiveRecord::Base
devise :database_authenticatable, :validatable, :confirmable
end
elsif DEVISE_ORM == :mongoid
class Octopussy
include Mongoid::Document
devise :database_authenticatable, :validatable, :confirmable
end
end
5 changes: 1 addition & 4 deletions spec/rails_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

module RailsApp
class Application < Rails::Application
# Add additional load paths for your own custom dirs
config.autoload_paths.reject!{ |p| p =~ /\/app\/(\w+)$/ && !%w(controllers helpers views).include?($1) }
config.autoload_paths += [ "#{config.root}/app/#{DEVISE_ORM}" ]
config.autoload_paths += %W[#{config.root}/app/#{DEVISE_ORM}]

# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters << :password

config.action_mailer.default_url_options = { :host => "localhost:3000" }
Expand Down
38 changes: 25 additions & 13 deletions spec/rails_app/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
# Tell if authentication through HTTP Basic Auth is enabled. True by default.
# config.http_authenticatable = true

# Set this to true to use Basic Auth for AJAX requests. True by default.
# config.http_authenticatable_on_xhr = true

# The realm used in Http Basic Authentication
# config.http_authentication_realm = "Application"

Expand All @@ -44,23 +47,36 @@

# Setup a pepper to generate the encrypted password.
config.pepper = "d142367154e5beacca404b1a6a4f8bc52c6fdcfa3ccc3cf8eb49f3458a688ee6ac3b9fae488432a3bfca863b8a90008368a9f3a3dfbe5a962e64b6ab8f3a3a1a"

# ==> Configuration for :invitable
# Time interval where the invitation token is valid.
# If invite_for is 0 or nil, the invitation will never expire.
# Default: 0
# config.invite_for = 2.weeks

# Flag that force a record to be valid before being actually invited.
# Default: false
# config.validate_on_invite = true

# ==> Configuration for :confirmable
# The time you want to give your user to confirm his account. During this time
# he will be able to access your application without confirming. Default is nil.
# When confirm_within is zero, the user won't be able to sign in without confirming.
# You can use this to let your user access some features of your application
# without confirming the account, but blocking it after a certain period
# (ie 2 days).
# When confirm_within is zero, the user won't be able to sign in without confirming.
# You can use this to let your user access some features of your application
# without confirming the account, but blocking it after a certain period
# (ie 2 days).
# config.confirm_within = 2.days

# ==> Configuration for :rememberable
# The time the user will be remembered without asking for credentials again.
# config.remember_for = 2.weeks

# If a valid remember token can be re-used between multiple browsers.
# If true, a valid remember token can be re-used between multiple browsers.
# config.remember_across_browsers = true

# If true, extends the user's remember period when remembered via cookie.
# config.extend_remember_period = false

# ==> Configuration for :validatable
# Range for password length
config.password_length = 6..20
Expand Down Expand Up @@ -99,15 +115,11 @@

# ==> Scopes configuration
# Turn scoped views on. Before rendering "sessions/new", it will first check for
# "sessions/users/new". It's turned off by default because it's slower if you
# "users/sessions/new". It's turned off by default because it's slower if you
# are using only default views.
# config.scoped_views = true

# Configure the default scope given to Warden. By default it's the first
# devise role declared in your routes.
# config.default_scope = :user
config.scoped_views = true

# Configure sign_out behavior.
# Configure sign_out behavior.
# By default sign_out is scoped (i.e. /users/sign_out affects only :user scope).
# In case of sign_out_all_scopes set to true any logout action will sign out all active scopes.
# config.sign_out_all_scopes = false
Expand All @@ -133,4 +145,4 @@
# end
# manager.default_strategies(:scope => :user).unshift :twitter_oauth
# end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
config.include Shoulda::ActionController::Matchers

config.filter_run :focus => true
config.filter_run_excluding :slow => true
config.run_all_when_everything_filtered = true

config.mock_with :rspec
Expand Down

0 comments on commit 443fe59

Please sign in to comment.