mirror of
https://github.com/kbparagua/paloma
synced 2023-03-27 23:21:17 -04:00
Refactored and Tested generators
This commit is contained in:
parent
53d7737b9b
commit
560cdc11ff
17 changed files with 201 additions and 176 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,6 +1,7 @@
|
|||
Gemfile.lock
|
||||
*.gem
|
||||
/tmp/*
|
||||
/spec/sample_app/tmp/*
|
||||
/spec/sample_app/log
|
||||
/spec/tmp/
|
||||
/spec/sample_app/tmp/
|
||||
/spec/sample_app/log/
|
||||
/spec/sample_app/paloma_test
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
//= require ./fake_controller/_local
|
||||
//= require_tree .
|
|
@ -1,20 +0,0 @@
|
|||
Paloma.fake_controller = {
|
||||
|
||||
/*
|
||||
Put here code which can be used by all callbacks
|
||||
under the fake_controller folder.
|
||||
|
||||
Example:
|
||||
|
||||
variableName: value;
|
||||
|
||||
functionName: function(params){
|
||||
alert('Paloma');
|
||||
};
|
||||
|
||||
To use the variable and function:
|
||||
Paloma.fake_controller.variableName
|
||||
Paloma.fake_controller.functionName(params);
|
||||
*/
|
||||
|
||||
};
|
|
@ -1,5 +0,0 @@
|
|||
Paloma.callbacks['fake_controller/fake_action'] = function(params){
|
||||
|
||||
// alert('Paloma');
|
||||
|
||||
};
|
|
@ -1,3 +0,0 @@
|
|||
|
||||
//= require ./users/_callbacks
|
||||
//= require ./fake_controller/_callbacks
|
|
@ -1,2 +0,0 @@
|
|||
//= require ./users/_local
|
||||
//= require_tree .
|
|
@ -1,20 +0,0 @@
|
|||
Paloma.users = {
|
||||
|
||||
/*
|
||||
Put here code which can be used by all callbacks
|
||||
under the users folder.
|
||||
|
||||
Example:
|
||||
|
||||
variableName: value;
|
||||
|
||||
functionName: function(params){
|
||||
alert('Paloma');
|
||||
};
|
||||
|
||||
To use the variable and function:
|
||||
Paloma.users.variableName
|
||||
Paloma.users.functionName(params);
|
||||
*/
|
||||
|
||||
};
|
|
@ -1,6 +1,23 @@
|
|||
module Paloma
|
||||
mattr_accessor :destination, :templates
|
||||
|
||||
def self.root
|
||||
@paloma_root ||= File.dirname(__FILE__) + '/../'
|
||||
@paloma_root ||= "#{File.dirname(__FILE__)}/../"
|
||||
end
|
||||
|
||||
|
||||
def self.index_js
|
||||
@index_js ||= "#{Paloma.destination}/index.js"
|
||||
end
|
||||
|
||||
|
||||
def self.destination
|
||||
@destination ||= 'app/assets/javascripts/paloma'
|
||||
end
|
||||
|
||||
|
||||
def self.templates
|
||||
@templates ||= "#{Paloma.root}/app/templates"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -9,6 +26,7 @@ require 'rails/generators'
|
|||
|
||||
# TODO: Rails version checking
|
||||
|
||||
require 'paloma/paloma_generator'
|
||||
require 'paloma/generators/add_generator'
|
||||
require 'paloma/generators/setup_generator'
|
||||
require 'paloma/action_controller_filters'
|
||||
require 'paloma/action_controller_extension'
|
||||
|
|
69
lib/paloma/generators/add_generator.rb
Normal file
69
lib/paloma/generators/add_generator.rb
Normal file
|
@ -0,0 +1,69 @@
|
|||
module Paloma
|
||||
#
|
||||
# Usage:
|
||||
# rails g paloma:add <controller_name>
|
||||
# - Generates the following:
|
||||
# - a folder under app/assets/javascripts/paloma named as <controller_name>
|
||||
# - callbacks.js inside the folder just made
|
||||
# - Updates index.js under the 'paloma' folder
|
||||
# - adds a line in order to require the callbacks to be made under the recently made folder
|
||||
#
|
||||
#
|
||||
# rails g paloma:add <controller_name>/<action_name>
|
||||
# - Generates the following:
|
||||
# - <action_name>.js file inside the <controller_name> folder
|
||||
#
|
||||
#
|
||||
# Generated Files:
|
||||
# <controller_name>/callbacks.js
|
||||
# - contains code for requiring all callbacks under the same folder <controller_name>
|
||||
#
|
||||
# <controller_name>/<action_name>.js
|
||||
# - actual code to be executed when callback is called
|
||||
#
|
||||
|
||||
class AddGenerator < ::Rails::Generators::NamedBase
|
||||
source_root Paloma.templates
|
||||
|
||||
def create_callback_file
|
||||
arg = file_path.split('/')
|
||||
@controller_name = arg[0]
|
||||
action_name = arg[1]
|
||||
|
||||
controller_folder = "#{Paloma.destination}/#{@controller_name}/"
|
||||
callbacks_js = "#{controller_folder}/_callbacks.js"
|
||||
local_js = "#{controller_folder}/_local.js"
|
||||
action_js = "#{controller_folder}/#{action_name}.js"
|
||||
|
||||
Dir.mkdir(controller_folder) unless Dir.exists?(controller_folder)
|
||||
|
||||
generate_from_template local_js unless File.exists?(local_js)
|
||||
generate_from_template callbacks_js unless File.exists?(callbacks_js)
|
||||
|
||||
# Create a js file for action if there is an action argument
|
||||
if action_name.present? && !File.exists?(action_js)
|
||||
content = File.read("#{Paloma.templates}/action.js").gsub(
|
||||
/controller\/action/,
|
||||
"#{@controller_name}/#{action_name}")
|
||||
|
||||
File.open(action_js, 'w'){ |f| f.write(content) }
|
||||
end
|
||||
|
||||
# Require controller's _callbacks.js to Paloma's main index.js file.
|
||||
# Located on "#{Paloma.destination}/index.js" or by default on
|
||||
# app/assets/javascripts/paloma/index.js
|
||||
File.open(Paloma.index_js, 'a+'){ |f|
|
||||
f << "\n//= require ./#{@controller_name}/_callbacks"
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def generate_from_template destination_filename
|
||||
filename = destination_filename.split('/').last
|
||||
content = File.read("#{Paloma.templates}/_local.js").gsub(/controller/, @controller_name)
|
||||
File.open(destination_filename, 'w'){ |f| f.write(content) }
|
||||
end
|
||||
end
|
||||
end
|
28
lib/paloma/generators/setup_generator.rb
Normal file
28
lib/paloma/generators/setup_generator.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
module Paloma
|
||||
#
|
||||
# rails g paloma:setup
|
||||
# - Generates the following:
|
||||
# - 'paloma' folder under app/assets/javascripts/
|
||||
# - index.js and paloma.js under the 'paloma' folder
|
||||
#
|
||||
# Generated Files:
|
||||
# index.js
|
||||
# - contains code for requiring all callbacks of all folders
|
||||
# - always updated when new folders and callback.js files are created
|
||||
#
|
||||
# paloma.js
|
||||
# - declaration of namespace used in all callbacks
|
||||
#
|
||||
|
||||
class SetupGenerator < ::Rails::Generators::Base
|
||||
source_root Paloma.templates
|
||||
|
||||
def setup_paloma
|
||||
index_js = "#{Paloma.destination}/index.js"
|
||||
paloma_js = "#{Paloma.destination}/paloma.js"
|
||||
|
||||
copy_file 'index.js', index_js unless File.exists?(index_js)
|
||||
copy_file 'paloma.js', paloma_js unless File.exists?(paloma_js)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,97 +0,0 @@
|
|||
module Paloma
|
||||
require 'fileutils'
|
||||
|
||||
#
|
||||
# Setup:
|
||||
# rails g paloma:setup
|
||||
# - Generates the following:
|
||||
# - 'paloma' folder under app/assets/javascripts/paloma
|
||||
# - index.js and paloma.js under the 'paloma' folder
|
||||
#
|
||||
#
|
||||
# Usage:
|
||||
# rails g paloma:add <controller_name>
|
||||
# - Generates the following:
|
||||
# - a folder under app/assets/javascripts/paloma named as <controller_name>
|
||||
# - callbacks.js inside the folder just made
|
||||
# - Updates index.js under the 'paloma' folder
|
||||
# - adds a line in order to require the callbacks to be made under the recently made folder
|
||||
#
|
||||
#
|
||||
# rails g paloma:add <controller_name>/<action_name>
|
||||
# - Generates the following:
|
||||
# - <action_name>.js file inside the <controller_name> folder
|
||||
#
|
||||
#
|
||||
# Generated Files:
|
||||
# index.js
|
||||
# - contains code for requiring all callbacks of all folders
|
||||
# - always updated when new folders and callback.js files are created
|
||||
#
|
||||
# paloma.js
|
||||
# - declaration of namespace used in all callbacks
|
||||
#
|
||||
# <controller_name>/callbacks.js
|
||||
# - contains code for requiring all callbacks under the same folder <controller_name>
|
||||
#
|
||||
# <controller_name>/<action_name>.js
|
||||
# - actual code to be executed when callback is called
|
||||
#
|
||||
|
||||
PARENT = 'app/assets/javascripts/paloma/'
|
||||
INDEX = PARENT + 'index.js'
|
||||
PALOMA = PARENT + 'paloma.js'
|
||||
|
||||
class SetupGenerator < ::Rails::Generators::Base
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def setup_paloma
|
||||
#index.js on callbacks folder
|
||||
has_index = File.exists? INDEX
|
||||
copy_file 'index.js', INDEX unless has_index
|
||||
|
||||
has_paloma = File.exists? PALOMA
|
||||
copy_file 'paloma.js', PALOMA unless has_paloma
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
class AddGenerator < ::Rails::Generators::NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def create_callback_file
|
||||
file = file_path.split('/')
|
||||
controller = file[0]
|
||||
action = file[1]
|
||||
|
||||
callbacks = PARENT + "#{controller}/_callbacks.js"
|
||||
templates = "#{Paloma.root}/lib/paloma/templates/"
|
||||
local = PARENT + "#{controller}/_local.js"
|
||||
action_file = PARENT + "#{controller}/#{action}.js"
|
||||
|
||||
#_callbacks.js per folder(controller)
|
||||
has_callbacks = File.exists? callbacks
|
||||
unless has_callbacks
|
||||
FileUtils.mkpath(PARENT + "#{controller}")
|
||||
content = File.read(templates + '_callbacks.js').gsub('controller', "#{controller}")
|
||||
File.open(callbacks, 'w'){ |f| f.write(content) }
|
||||
File.open(INDEX, 'a+'){ |f| f << "\n//= require ./" + controller + '/_callbacks' }
|
||||
end
|
||||
|
||||
#_local.js per folder(controller)
|
||||
has_local =File.exists? local
|
||||
unless has_local
|
||||
content = File.read(templates + '_local.js').gsub('controller', "#{controller}")
|
||||
File.open(local, 'w'){ |f| f.write(content) }
|
||||
end
|
||||
|
||||
#<action>.js on folder(controller) - code for callback
|
||||
has_action = File.exists? action_file
|
||||
unless (action.nil? || has_action)
|
||||
content = File.read(templates + 'action.js').gsub('controller/action', "#{controller}/#{action}")
|
||||
File.open(action_file, 'w'){ |f| f.write(content) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,9 +2,11 @@ require 'spec_helper'
|
|||
require 'generator_spec/test_case'
|
||||
require 'fileutils'
|
||||
|
||||
TEMP = "#{File.dirname(__FILE__)}/tmp/"
|
||||
|
||||
feature Paloma::SetupGenerator do
|
||||
include GeneratorSpec::TestCase
|
||||
destination "#{Rails.root}/tmp"
|
||||
destination TEMP
|
||||
|
||||
before do
|
||||
prepare_destination
|
||||
|
@ -13,43 +15,99 @@ feature Paloma::SetupGenerator do
|
|||
|
||||
specify do
|
||||
destination_root.should have_structure {
|
||||
directory 'app' do
|
||||
directory 'assets' do
|
||||
directory 'javascripts' do
|
||||
directory 'paloma' do
|
||||
file 'paloma.js'
|
||||
file 'index.js'
|
||||
end
|
||||
end
|
||||
end
|
||||
directory Paloma.destination do
|
||||
file 'paloma.js'
|
||||
file 'index.js'
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
feature Paloma::AddGenerator do
|
||||
|
||||
def mimic_setup
|
||||
# Mimic SetupGenerator results before running the AddGenerator
|
||||
FileUtils.cd TEMP
|
||||
FileUtils.mkpath Paloma.destination
|
||||
File.open("#{Paloma.destination}/index.js", 'w') { |f| f.write('// test')}
|
||||
end
|
||||
|
||||
|
||||
|
||||
feature Paloma::AddGenerator, 'creating controller folder only' do
|
||||
include GeneratorSpec::TestCase
|
||||
destination "#{Rails.root}/tmp"
|
||||
arguments ['fake_controller/fake_action']
|
||||
destination TEMP
|
||||
arguments ['sexy_controller']
|
||||
|
||||
before do
|
||||
prepare_destination
|
||||
mimic_setup
|
||||
run_generator
|
||||
end
|
||||
|
||||
|
||||
specify do
|
||||
destination_root.should have_structure {
|
||||
directory 'app' do
|
||||
directory 'assets' do
|
||||
directory 'javascripts' do
|
||||
directory 'paloma' do
|
||||
directory 'fake_controller' do
|
||||
file 'fake_action.js'
|
||||
end
|
||||
end
|
||||
directory Paloma.destination do
|
||||
directory 'sexy_controller'
|
||||
|
||||
file 'index.js' do
|
||||
contains '//= require ./sexy_controller/_callbacks'
|
||||
end
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
feature Paloma::AddGenerator, 'creating action with existing controller folder' do
|
||||
include GeneratorSpec::TestCase
|
||||
destination TEMP
|
||||
arguments ['existing_controller_folder/new_action']
|
||||
|
||||
before do
|
||||
prepare_destination
|
||||
mimic_setup
|
||||
Dir.mkdir "#{Paloma.destination}/existing_controller_folder"
|
||||
|
||||
run_generator
|
||||
end
|
||||
|
||||
specify do
|
||||
destination_root.should have_structure {
|
||||
directory Paloma.destination do
|
||||
directory 'existing_controller_folder' do
|
||||
file 'new_action.js' do
|
||||
contains "Paloma.callbacks['existing_controller_folder/new_action']"
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
feature Paloma::AddGenerator, 'creating both controller folder and action file' do
|
||||
include GeneratorSpec::TestCase
|
||||
destination TEMP
|
||||
arguments ['new_controller_folder/new_action']
|
||||
|
||||
before do
|
||||
prepare_destination
|
||||
mimic_setup
|
||||
run_generator
|
||||
end
|
||||
|
||||
specify do
|
||||
destination_root.should have_structure {
|
||||
directory Paloma.destination do
|
||||
directory 'new_controller_folder' do
|
||||
file 'new_action.js' do
|
||||
contains "Paloma.callbacks['new_controller_folder/new_action']"
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue