diff --git a/.gitignore b/.gitignore index ae8c235..a72e23f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/app/assets/javascripts/paloma/fake_controller/_callbacks.js b/app/assets/javascripts/paloma/fake_controller/_callbacks.js deleted file mode 100644 index f2bdfe7..0000000 --- a/app/assets/javascripts/paloma/fake_controller/_callbacks.js +++ /dev/null @@ -1,2 +0,0 @@ -//= require ./fake_controller/_local -//= require_tree . diff --git a/app/assets/javascripts/paloma/fake_controller/_local.js b/app/assets/javascripts/paloma/fake_controller/_local.js deleted file mode 100644 index ab3d0c1..0000000 --- a/app/assets/javascripts/paloma/fake_controller/_local.js +++ /dev/null @@ -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); - */ - -}; diff --git a/app/assets/javascripts/paloma/fake_controller/fake_action.js b/app/assets/javascripts/paloma/fake_controller/fake_action.js deleted file mode 100644 index f44e06d..0000000 --- a/app/assets/javascripts/paloma/fake_controller/fake_action.js +++ /dev/null @@ -1,5 +0,0 @@ -Paloma.callbacks['fake_controller/fake_action'] = function(params){ - - // alert('Paloma'); - -}; diff --git a/app/assets/javascripts/paloma/index.js b/app/assets/javascripts/paloma/index.js deleted file mode 100644 index fac91c8..0000000 --- a/app/assets/javascripts/paloma/index.js +++ /dev/null @@ -1,3 +0,0 @@ - -//= require ./users/_callbacks -//= require ./fake_controller/_callbacks \ No newline at end of file diff --git a/app/assets/javascripts/paloma/users/_callbacks.js b/app/assets/javascripts/paloma/users/_callbacks.js deleted file mode 100644 index 636ac19..0000000 --- a/app/assets/javascripts/paloma/users/_callbacks.js +++ /dev/null @@ -1,2 +0,0 @@ -//= require ./users/_local -//= require_tree . diff --git a/app/assets/javascripts/paloma/users/_local.js b/app/assets/javascripts/paloma/users/_local.js deleted file mode 100644 index bef71d6..0000000 --- a/app/assets/javascripts/paloma/users/_local.js +++ /dev/null @@ -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); - */ - -}; diff --git a/lib/paloma/templates/_callbacks.js b/app/templates/_callbacks.js similarity index 100% rename from lib/paloma/templates/_callbacks.js rename to app/templates/_callbacks.js diff --git a/lib/paloma/templates/_local.js b/app/templates/_local.js similarity index 100% rename from lib/paloma/templates/_local.js rename to app/templates/_local.js diff --git a/lib/paloma/templates/action.js b/app/templates/action.js similarity index 100% rename from lib/paloma/templates/action.js rename to app/templates/action.js diff --git a/lib/paloma/templates/index.js b/app/templates/index.js similarity index 100% rename from lib/paloma/templates/index.js rename to app/templates/index.js diff --git a/lib/paloma/templates/paloma.js b/app/templates/paloma.js similarity index 100% rename from lib/paloma/templates/paloma.js rename to app/templates/paloma.js diff --git a/lib/paloma.rb b/lib/paloma.rb index 051f227..863008b 100644 --- a/lib/paloma.rb +++ b/lib/paloma.rb @@ -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' diff --git a/lib/paloma/generators/add_generator.rb b/lib/paloma/generators/add_generator.rb new file mode 100644 index 0000000..c18f4b3 --- /dev/null +++ b/lib/paloma/generators/add_generator.rb @@ -0,0 +1,69 @@ +module Paloma + # + # Usage: + # rails g paloma:add + # - Generates the following: + # - a folder under app/assets/javascripts/paloma named as + # - 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 / + # - Generates the following: + # - .js file inside the folder + # + # + # Generated Files: + # /callbacks.js + # - contains code for requiring all callbacks under the same folder + # + # /.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 diff --git a/lib/paloma/generators/setup_generator.rb b/lib/paloma/generators/setup_generator.rb new file mode 100644 index 0000000..3d9428d --- /dev/null +++ b/lib/paloma/generators/setup_generator.rb @@ -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 diff --git a/lib/paloma/paloma_generator.rb b/lib/paloma/paloma_generator.rb deleted file mode 100644 index 4b72220..0000000 --- a/lib/paloma/paloma_generator.rb +++ /dev/null @@ -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 - # - Generates the following: - # - a folder under app/assets/javascripts/paloma named as - # - 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 / - # - Generates the following: - # - .js file inside the 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 - # - # /callbacks.js - # - contains code for requiring all callbacks under the same folder - # - # /.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 - - #.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 diff --git a/spec/generator_spec.rb b/spec/generator_spec.rb index 85a5fae..96219a9 100644 --- a/spec/generator_spec.rb +++ b/spec/generator_spec.rb @@ -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