├── install.rb ├── uninstall.rb ├── init.rb ├── test ├── test_helper.rb └── once_test.rb ├── tasks └── once_tasks.rake ├── Rakefile ├── lib └── once.rb ├── MIT-LICENSE └── README /install.rb: -------------------------------------------------------------------------------- 1 | # Install hook code here 2 | -------------------------------------------------------------------------------- /uninstall.rb: -------------------------------------------------------------------------------- 1 | # Uninstall hook code here 2 | -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), "lib", "once") 2 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'active_support' 3 | require 'active_support/test_case' -------------------------------------------------------------------------------- /tasks/once_tasks.rake: -------------------------------------------------------------------------------- 1 | # desc "Explaining what the task does" 2 | # task :once do 3 | # # Task goes here 4 | # end 5 | -------------------------------------------------------------------------------- /test/once_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class OnceTest < ActiveSupport::TestCase 4 | # Replace this with your real tests. 5 | test "the truth" do 6 | assert true 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rake/rdoctask' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the once plugin.' 9 | Rake::TestTask.new(:test) do |t| 10 | t.libs << 'lib' 11 | t.libs << 'test' 12 | t.pattern = 'test/**/*_test.rb' 13 | t.verbose = true 14 | end 15 | 16 | desc 'Generate documentation for the once plugin.' 17 | Rake::RDocTask.new(:rdoc) do |rdoc| 18 | rdoc.rdoc_dir = 'rdoc' 19 | rdoc.title = 'Once' 20 | rdoc.options << '--line-numbers' << '--inline-source' 21 | rdoc.rdoc_files.include('README') 22 | rdoc.rdoc_files.include('lib/**/*.rb') 23 | end 24 | -------------------------------------------------------------------------------- /lib/once.rb: -------------------------------------------------------------------------------- 1 | # Once 2 | 3 | class ActionController::Base 4 | before_filter :clear_once_actions 5 | 6 | def clear_once_actions 7 | ActionView::Helpers::Once::clear_actions 8 | end 9 | end 10 | 11 | module ActionView 12 | module Helpers 13 | class Once 14 | @@actions = {} 15 | 16 | def self.do(name, &block) 17 | if !self.done?(name) 18 | self.done(name) 19 | if Rails::VERSION::MAJOR == 3 20 | yield 21 | nil 22 | else 23 | return yield 24 | end 25 | end 26 | nil 27 | end 28 | 29 | def self.done(name) 30 | @@actions[name] = true 31 | end 32 | 33 | def self.done?(name) 34 | @@actions[name] 35 | end 36 | 37 | def self.clear_actions 38 | @@actions = {} 39 | end 40 | end 41 | 42 | def once(name, &block) 43 | Once::do(name, &block) 44 | end 45 | 46 | def once_done?(name) 47 | Once::done?(name) 48 | end 49 | 50 | def once_done(name) 51 | Once::done(name) 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 [name of plugin creator] 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Once 2 | ==== 3 | 4 | Imagine situation where you have to call the code in many layouts/partials/views and code must be executed only once. 5 | Can't ? :), see example 6 | 7 | Example 8 | ======= 9 | 10 | You need to load jquery (or any other javascript library) in two different partials ('_pagination.haml' and '_image_gallery.haml') 11 | You can include 12 | =javascript_include_tag 'jquery' 13 | in partials. If both are rendered on the page there will be duplication of 14 | 15 | 16 | The solution is "Once" :) 17 | Instead of 18 | =javascript_include_tag 'jquery' 19 | You write 20 | =once :load_jquery { javascript_include_tag 'jquery' } 21 | 22 | You can simplify jquery loading by defining helper in app/helpers/application_helper.erb 23 | 24 | def javascript_include_jquery 25 | once(:load_jquery) { 26 | javascript_include_tag 'jquery.min' 27 | } 28 | end 29 | 30 | or even 31 | 32 | def javascript_require_jquery 33 | once(:load_jquery) do 34 | content_for :script do 35 | javascript_include_tag 'jquery' 36 | end 37 | end 38 | end 39 | 40 | 41 | 42 | 43 | Copyright (c) 2009 Olexiy Zamkoviy, released under the MIT license 44 | --------------------------------------------------------------------------------