Permalink
Browse files

initial commit

  • Loading branch information...
1 parent 8b30f2c commit 431ddc99a91aac7209eb00f62c7a41b8f1d9e6f9 @pgte committed Oct 9, 2009
Showing with 668 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +3 −0 README.markdown
  3. +23 −0 Rakefile
  4. +17 −0 app/controllers/styledobjects_controller.rb
  5. +3 −0 config/routes.rb
  6. +16 −0 init.rb
  7. +1 −0 install.rb
  8. +66 −0 lib/styled_objects/actionview.rb
  9. +5 −0 lib/styled_objects/class_name_builder.rb
  10. +27 −0 lib/styled_objects/generator.rb
  11. +18 −0 lib/styled_objects/helpers/stylesheet_link_tag.rb
  12. +23 −0 lib/styled_objects/html/decorator.rb
  13. +38 −0 lib/styled_objects/partials_collector.rb
  14. +4 −0 tasks/styled_objects_tasks.rake
  15. +16 −0 test/actionview_test.rb
  16. +2 −0 test/app_root/app/controllers/application_controller.rb
  17. +7 −0 test/app_root/app/controllers/test_controller.rb
  18. +115 −0 test/app_root/config/boot.rb
  19. +31 −0 test/app_root/config/database.yml
  20. +16 −0 test/app_root/config/environment.rb
  21. 0 test/app_root/config/environments/in_memory.rb
  22. 0 test/app_root/config/environments/mysql.rb
  23. 0 test/app_root/config/environments/postgresql.rb
  24. 0 test/app_root/config/environments/sqlite.rb
  25. 0 test/app_root/config/environments/sqlite3.rb
  26. +3 −0 test/app_root/config/routes.rb
  27. +4 −0 test/app_root/lib/console_with_fixtures.rb
  28. +120 −0 test/app_root/log/in_memory.log
  29. 0 test/app_root/public/styledobjects/.css
  30. +2 −0 test/app_root/public/styledobjects/test/_foo:test/_bar:test2/_boom.css.css
  31. +4 −0 test/app_root/script/about
  32. +3 −0 test/app_root/script/console
  33. +3 −0 test/app_root/script/dbconsole
  34. +3 −0 test/app_root/script/destroy
  35. +3 −0 test/app_root/script/generate
  36. +3 −0 test/app_root/script/plugin
  37. +3 −0 test/app_root/script/runner
  38. +3 −0 test/app_root/script/server
  39. +19 −0 test/class_name_builder_test.rb
  40. +22 −0 test/generator_test.rb
  41. +21 −0 test/test_helper.rb
  42. +1 −0 uninstall.rb
View
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,3 @@
+= Install =
+
+<code>script/plugin install git://github.com/pgte/styled_objects.git</code>
View
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the styled_objects plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'test'
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the styled_objects plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Styled-objects'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,17 @@
+class StyledobjectsController < ApplicationController
+ caches_page :show
+
+ def show
+ path_spec = params[:id]
+
+ headers['Cache-Control'] = 'public; max-age=2592000'
+ render :text => StyledObjects::Generator.generate(view_paths, params[:id].join('/')), :content_type => "text/css"
+ end
+
+ private
+
+ # Don't log.
+ def logger
+ nil
+ end
+end
View
@@ -0,0 +1,3 @@
+ActionController::Routing::Routes.draw do |map|
+ map.styledobject "styledobjects/*id.css", :controller => 'styledobjects', :action => "show"
+end
View
@@ -0,0 +1,16 @@
+begin
+ require 'less'
+rescue LoadError => e
+ e.message << " (You may need to install the less gem)"
+ raise e
+end
+
+require 'styled_objects/partials_collector'
+require 'styled_objects/generator'
+require 'styled_objects/actionview'
+require 'styled_objects/class_name_builder'
+require 'styled_objects/html/decorator'
+require 'styled_objects/helpers/stylesheet_link_tag'
+ActionView::Base.class_eval {include StyledObjects::ActionView::Base}
+ActionView::Base.class_eval {include StyledObjects::HTML::Decorator}
+ActionView::Base.class_eval {include StyledObjects::Helpers::StylesheetLinkTag}
View
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1,66 @@
+module StyledObjects
+ module ActionView
+ module Base
+ def self.included(base)
+ base.send(:include, InstanceMethods)
+ end
+
+ module InstanceMethods
+
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :render, :styled_object unless base.respond_to? :render_with_styled_object
+ end
+ end
+
+ def render_with_styled_object(options = {}, local_assigns = {}, &block)
+
+ #render_without_styled_object (options, local_assigns, &block)
+ begin
+ RAILS_DEFAULT_LOGGER.debug("render_with_styled_object with file: #{options[:file].path_without_format_and_extension if options[:file]} and partial #{options[:partial]}")
+ RAILS_DEFAULT_LOGGER.debug caller
+ @partials_collector ||= ::StyledObjects::PartialsCollector.new(view_paths)
+ decorated_partial = !options[:partial].nil? && template_format == :html
+
+ if decorated_partial && @partials_collector << partial_full_path = _so_pick_partial_template_path(options[:partial])
+ so_decorate partial_full_path do
+ render_without_styled_object(options, local_assigns, &block)
+ end
+ else
+ render_without_styled_object (options, local_assigns, &block)
+ end
+ rescue => exception
+ RAILS_DEFAULT_LOGGER.error 'EXCCEPT'+exception.to_s
+ end
+
+ end
+
+ protected
+
+ def styled_objects_partial_css_paths
+ (@partials_collector.partials if @partials_collector) || []
+ end
+
+
+ private
+
+ def _so_pick_partial_template_path(partial_path)
+ if partial_path.include?('/')
+ path = File.join(File.dirname(partial_path), "_#{File.basename(partial_path)}")
+ elsif controller
+ path = "#{controller.class.controller_path}/_#{partial_path}"
+ else
+ path = "_#{partial_path}"
+ end
+ end
+
+ def _translate_partial_path_into_class(partial_path)
+ "so-#{partial_path.gsub(/-/, '--').gsub(/\//, '-').gsub(/_/, '')}"
+ end
+
+ end
+
+ end
+
+ end
+end
@@ -0,0 +1,5 @@
+class StyledObjects::ClassNameBuilder
+ def self.build_class_name_from_partial_path(partial_path)
+ "so-#{partial_path.gsub(/-/, '--').gsub(/\//, '-').gsub(/_|\.css|\/\/|\.\./, '')}"
+ end
+end
@@ -0,0 +1,27 @@
+class StyledObjects::Generator
+
+ def self.generate(view_paths, uri)
+ content = uri.split(':').collect do |path|
+ path.gsub!(/\.\.|\/\//, '')
+ file_path = self._find_partial_stylesheet(view_paths, "#{path}.css")
+ if file_path
+ File.open(file_path) do |file|
+ ".#{StyledObjects::ClassNameBuilder.build_class_name_from_partial_path(path)}{#{file.read}}"
+ end
+ end
+ end.compact.join("\n")
+ ::Less::Engine.new(content).to_css
+ end
+
+ private
+
+ def self._find_partial_stylesheet(paths, path)
+ for view_path in paths
+ try_path = File.join view_path, path
+ return try_path if File.file? try_path
+ end
+ nil
+
+ end
+
+end
@@ -0,0 +1,18 @@
+module StyledObjects
+ module Helpers
+ module StylesheetLinkTag
+
+ def self.included(base)
+ base.send(:include, InstanceMethods)
+ end
+
+ module InstanceMethods
+ def so_stylesheet_link_tag
+ return "<link href=\"#{styledobject_path styled_objects_partial_css_paths.collect {|path| URI.encode(path.gsub(/:|\.css/, ''))}.join(':')}\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"
+ end
+ end
+
+ end
+ end
+end
+
@@ -0,0 +1,23 @@
+module StyledObjects
+ module HTML
+ module Decorator
+
+ def self.included(base)
+ base.send(:include, InstanceMethods)
+ end
+
+ module InstanceMethods
+
+ protected
+
+ def so_decorate(partial_path, &block)
+ "<div class=\"#{StyledObjects::ClassNameBuilder.build_class_name_from_partial_path(partial_path)}\">#{yield}</div>"
+ end
+
+ private
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,38 @@
+module StyledObjects
+ class PartialsCollector
+
+ def initialize(view_paths)
+ @partials = {}
+ @view_paths = view_paths
+ end
+
+ def partial_visited(partial)
+ style_file = _find_style_file(partial)
+ if style_file
+ @partials[style_file] = true
+ else
+ nil
+ end
+ end
+
+ def << (partial)
+ partial_visited(partial)
+ end
+
+ def partials
+ @partials.keys
+ end
+
+ private
+
+ def _find_style_file(partial)
+ style_file = "#{partial}.css"
+ @view_paths.each do |view_path|
+ try_path = File.join view_path, style_file
+ return style_file if File.file? try_path
+ end
+ nil
+ end
+
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :styled_objects do
+# # Task goes here
+# end
@@ -0,0 +1,16 @@
+require 'test_helper'
+class ActionviewTest < ActionController::IntegrationTest
+
+ def setup
+ get '/'
+ end
+
+ def test_response
+ assert_response :success
+# assert_equal 'rurumba', @response.body
+ end
+
+# test "truth" do
+# assert true
+# end
+end
@@ -0,0 +1,2 @@
+class ApplicationController < ActionController::Base
+end
@@ -0,0 +1,7 @@
+class TestController < ApplicationController
+
+ def index
+ #render :action => 'index'
+ end
+
+end
Oops, something went wrong. Retry.

0 comments on commit 431ddc9

Please sign in to comment.