theme_park 0.1.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.md +105 -0
  3. data/Rakefile +16 -0
  4. data/lib/generators/templates/application.css +5 -0
  5. data/lib/generators/templates/application.js +3 -0
  6. data/lib/generators/templates/layouts/application.html.erb +14 -0
  7. data/lib/generators/templates/theme_park.rb +31 -0
  8. data/lib/generators/theme_park/create_generator.rb +28 -0
  9. data/lib/generators/theme_park/install_generator.rb +16 -0
  10. data/lib/tasks/theme_park.rake +33 -0
  11. data/lib/theme_park.rb +123 -0
  12. data/lib/theme_park/configuration.rb +47 -0
  13. data/lib/theme_park/rails/action_controller.rb +53 -0
  14. data/lib/theme_park/rails/asset_tag_helper.rb +78 -0
  15. data/lib/theme_park/rails/server.rb +38 -0
  16. data/lib/theme_park/railtie.rb +52 -0
  17. data/lib/theme_park/server.rb +25 -0
  18. data/lib/theme_park/version.rb +11 -0
  19. data/test/dummy/README.rdoc +261 -0
  20. data/test/dummy/Rakefile +7 -0
  21. data/test/dummy/app/assets/javascripts/application.js +15 -0
  22. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  23. data/test/dummy/app/controllers/application_controller.rb +3 -0
  24. data/test/dummy/app/helpers/application_helper.rb +2 -0
  25. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/test/dummy/app/views/test1/index.html.erb +1 -0
  27. data/test/dummy/config.ru +4 -0
  28. data/test/dummy/config/application.rb +56 -0
  29. data/test/dummy/config/boot.rb +10 -0
  30. data/test/dummy/config/database.yml +25 -0
  31. data/test/dummy/config/environment.rb +5 -0
  32. data/test/dummy/config/environments/development.rb +37 -0
  33. data/test/dummy/config/environments/production.rb +67 -0
  34. data/test/dummy/config/environments/test.rb +37 -0
  35. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/test/dummy/config/initializers/inflections.rb +15 -0
  37. data/test/dummy/config/initializers/mime_types.rb +5 -0
  38. data/test/dummy/config/initializers/secret_token.rb +7 -0
  39. data/test/dummy/config/initializers/session_store.rb +8 -0
  40. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/test/dummy/config/locales/en.yml +5 -0
  42. data/test/dummy/config/routes.rb +58 -0
  43. data/test/dummy/db/test.sqlite3 +0 -0
  44. data/test/dummy/log/test.log +1703 -0
  45. data/test/dummy/public/404.html +26 -0
  46. data/test/dummy/public/422.html +26 -0
  47. data/test/dummy/public/500.html +25 -0
  48. data/test/dummy/public/favicon.ico +0 -0
  49. data/test/dummy/script/rails +6 -0
  50. data/test/dummy/themes/default/assets/javascripts/default.js +0 -0
  51. data/test/dummy/themes/default/assets/stylesheets/default.css +0 -0
  52. data/test/dummy/themes/default/views/layouts/application.html.erb +1 -0
  53. data/test/dummy/themes/default/views/test/edit.html.erb +1 -0
  54. data/test/dummy/themes/default/views/test/index.html.erb +1 -0
  55. data/test/dummy/themes/default/views/test/show.html.erb +1 -0
  56. data/test/dummy/themes/default/views/test1/show.html.erb +1 -0
  57. data/test/dummy/themes/default1/views/layouts/application.html.erb +1 -0
  58. data/test/dummy/themes/default1/views/test/edit.html.erb +1 -0
  59. data/test/dummy/themes/default1/views/test/index.html.erb +1 -0
  60. data/test/dummy/themes/default1/views/test/show.html.erb +1 -0
  61. data/test/dummy/themes/default1/views/test1/edit.html.erb +1 -0
  62. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  63. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  64. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  65. data/test/helper.rb +28 -0
  66. data/test/rails/action_controller_test.rb +58 -0
  67. data/test/rails/action_view_test.rb +103 -0
  68. data/test/rails/server_test.rb +5 -0
  69. data/test/railtie_test.rb +32 -0
  70. data/test/server_test.rb +5 -0
  71. data/test/theme_park_test.rb +79 -0
  72. data/test/themes/default/test.js +0 -0
  73. data/test/themes/default1/test1.js +0 -0
  74. metadata +306 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Thierry Zires
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.
@@ -0,0 +1,105 @@
1
+ ## theme_park
2
+
3
+ theme_park is a flexible multiple theme plugin for rails3, sinatra, and more...it's rack based
4
+
5
+ ### Getting started (Rails.version > 3.1)
6
+
7
+ Add it to your Gemfile.
8
+
9
+ ```console
10
+ gem 'theme_park'
11
+ ```
12
+
13
+ Install it use bundler command.
14
+
15
+ ```console
16
+ bundle install
17
+ ```
18
+
19
+ You can create a initializer file.
20
+
21
+ ```console
22
+ rails generate theme_park:install
23
+ ```
24
+
25
+ Now, let's create our first theme.
26
+
27
+ ```console
28
+ rails generate theme_park:create dark
29
+ ```
30
+
31
+ It will create the theme dir under the theme_park root path.
32
+
33
+ ```
34
+ app/
35
+ - themes
36
+ - [theme_name]
37
+ |- assets
38
+ |- images
39
+ |- javascripts
40
+ |- stylesheets
41
+ |- compiled -> Compiled static files
42
+ |- views
43
+ |- layouts
44
+ ```
45
+
46
+ Use this theme in controller.
47
+
48
+ ```ruby
49
+ class MyController < ApplicationController
50
+ theme "dark", :only => :index
51
+ end
52
+ ```
53
+
54
+ ```ruby
55
+ class MyController < ApplicationController
56
+ def index
57
+ theme 'dark'
58
+ end
59
+ end
60
+ ```
61
+
62
+ Like rails3.1 need precompile asset files in the production environment, we also need precompile theme asset files.
63
+
64
+ ```console
65
+ rake theme_park:precompile theme=dark
66
+ ```
67
+
68
+ or precompile all themes
69
+
70
+ ```console
71
+ rake theme_park:precompile
72
+ ```
73
+
74
+ ### Configuration
75
+
76
+ See theme_park initializer file for more details.
77
+
78
+ ```
79
+ config.root = '/themes'
80
+ config.handlers = :all # will load all handlers depends on your application.
81
+ # other options for handlers
82
+ # config.handlers = [:liquid] # only load .liquid
83
+ # config.handlers = [:erb, :haml] # only load .erb and .haml
84
+ config.prefix = 'themes'
85
+ config.images_path = ':root/:name/assets/images'
86
+ config.javascripts_path = ':root/:name/assets/javascripts'
87
+ config.stylesheets_path = ':root/:name/assets/stylesheets'
88
+ config.compiled_path = ':root/:name/assets/compiled'
89
+ config.views_path = ':root/:name/views'
90
+ ```
91
+
92
+ ### Contributing to theme_park
93
+
94
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
95
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
96
+ * Fork the project.
97
+ * Start a feature/bugfix branch.
98
+ * Commit and push until you are happy with your contribution.
99
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
100
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
101
+
102
+ ### Copyright
103
+
104
+ Copyright (c) 2012 Thierry Zires. See LICENSE.txt for further details.
105
+
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.pattern = 'test/**/*_test.rb'
10
+ test.verbose = true
11
+ end
12
+
13
+ task :default => :test
14
+
15
+ require 'yard'
16
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,5 @@
1
+ /*
2
+ *
3
+ *= require_self
4
+ *= require_tree .
5
+ */
@@ -0,0 +1,3 @@
1
+ //
2
+ //
3
+ //= require_tree .
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title></title>
5
+ <%#= stylesheet_link_tag "application", :media => "all" %>
6
+ <%#= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,31 @@
1
+ # Convention over configuration.
2
+ #
3
+ # app/
4
+ # - themes
5
+ # - [theme_name]
6
+ # |- assets
7
+ # |- images
8
+ # |- javascripts
9
+ # |- stylesheets
10
+ # |- compiled -> Compiled static files
11
+ # |- views
12
+ # |- layouts
13
+ #
14
+ # Uncomment below lines if you really need to.
15
+ # ===
16
+ # ThemePark.setup do |config|
17
+ #
18
+ # config.root = '/themes'
19
+ # config.handlers = :all # will load all handlers depends on your application.
20
+ # # other options for handlers
21
+ # # config.handlers = [:liquid] # only load .liquid
22
+ # # config.handlers = [:erb, :haml] # only load .erb and .haml
23
+ # config.prefix = 'themes'
24
+ # config.images_path = ':root/:name/assets/images'
25
+ # config.javascripts_path = ':root/:name/assets/javascripts'
26
+ # config.stylesheets_path = ':root/:name/assets/stylesheets'
27
+ # config.compiled_path = ':root/:name/assets/compiled'
28
+ # config.views_path = ':root/:name/views'
29
+
30
+ # end
31
+ # ===
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require 'fileutils'
3
+
4
+ module ThemePark
5
+ module Generators
6
+ class CreateGenerator < ::Rails::Generators::Base
7
+ # Create a new theme under theme_park root path.
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+ argument :name, :type => :string
10
+
11
+ #class_option :force, :type => :boolean, :default => false, :alias => :f, :description => "Will cover exist files."
12
+
13
+ desc "Create an empty theme under theme_park root path."
14
+ def create_theme
15
+ ThemePark.theme_assets_path(name).each do |path|
16
+ FileUtils.mkdir_p path
17
+ end
18
+ FileUtils.mkdir_p ThemePark.views_path(name)
19
+ FileUtils.mkdir_p ThemePark.compiled_path(name)
20
+
21
+ copy_file "application.css", File.join( ThemePark.stylesheets_path(name), "#{name}.css" )
22
+ copy_file "application.js", File.join( ThemePark.javascripts_path(name), "#{name}.js" )
23
+ directory "layouts", File.join( ThemePark.views_path(name), "layouts" )
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ module ThemePark
3
+ module Generators
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ # Create theme_park.rb under config/initializers/
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a ThemePark initializer file."
9
+ def copy_initializer
10
+ template "theme_park.rb", "config/initializers/theme_park.rb"
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,33 @@
1
+ # Precompile your asset source file in compiled folder.
2
+ # Usage:
3
+ # rake theme_park:precompile theme=dark
4
+ # if theme_name not given, it will precompile all themes.
5
+ namespace :theme_park do
6
+
7
+ desc "Precompile your asset source file in compiled folder"
8
+ task :precompile => :environment do
9
+
10
+ unless ::Rails.application.config.assets.enabled
11
+ warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
12
+ exit
13
+ end
14
+
15
+ theme = ENV["theme"]
16
+ env = ::Rails.application.assets
17
+
18
+ themes = if theme
19
+ [ theme ]
20
+ else
21
+ Dir.glob( File.join(ThemePark.base_root, "*") ).map{ |path| File.basename(path) }
22
+ end
23
+
24
+ themes.each do |name|
25
+ target = ThemePark.compiled_path(name)
26
+ paths = [/(?:\/|\\|\A)#{name}\.(css|js)$/]
27
+ compiler = Sprockets::StaticCompiler.new(env, target, paths)
28
+ compiler.compile
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,123 @@
1
+ # encoding: utf-8
2
+ require 'active_support/core_ext/module/delegation'
3
+ #
4
+ # convention over configuration.
5
+ #
6
+ # Rails application example:
7
+ # app/
8
+ # - themes
9
+ # - [theme_name]
10
+ # |- assets
11
+ # |- images
12
+ # |- javascripts
13
+ # |- stylesheets
14
+ # |- compiled -> Compiled static files
15
+ # |- views
16
+ # |- layouts
17
+ #
18
+ module ThemePark
19
+ autoload :Configuration, 'theme_park/configuration'
20
+ autoload :Version, 'theme_park/version'
21
+ autoload :Server, 'theme_park/server'
22
+
23
+ module Rails
24
+ autoload :ActionController, 'theme_park/rails/action_controller'
25
+ autoload :AssetTagHelper, 'theme_park/rails/asset_tag_helper'
26
+ autoload :Server, 'theme_park/rails/server'
27
+ end
28
+
29
+ # Setup and return itself.
30
+ def self.setup
31
+ yield config if block_given?
32
+ self
33
+ end
34
+
35
+ class << self
36
+
37
+ delegate :base, :root, :base_root, :prefix, :handlers, :to => :config
38
+
39
+ def config
40
+ @@config ||= ThemePark::Configuration.new
41
+ end
42
+
43
+ def version
44
+ ThemePark::Version::STRING
45
+ end
46
+
47
+ # Return the path of given theme name.
48
+ def path(theme_name)
49
+ File.join base_root, theme_name
50
+ end
51
+
52
+ def exist?(theme_name)
53
+ Dir.exist? path(theme_name)
54
+ end
55
+
56
+ def interpolate(pattern, theme_name)
57
+ pattern.gsub(":root", base_root).gsub(":name", theme_name.to_s)
58
+ end
59
+
60
+ ##
61
+ # :method: images_path
62
+ #
63
+ # :call-seq: images_path(theme_name)
64
+ #
65
+ # Return images path for the theme name
66
+
67
+ ##
68
+ # :method: javascripts_path
69
+ #
70
+ # :call-seq: javascripts_path(theme_name)
71
+ #
72
+ # Return javascripts path for the theme name
73
+
74
+ ##
75
+ # :method: stylesheets_path
76
+ #
77
+ # :call-seq: stylesheets_path(theme_name)
78
+ #
79
+ # Return stylesheets path for the theme name
80
+
81
+ ##
82
+ # :method: compiled_path
83
+ #
84
+ # :call-seq: compiled_path(theme_name)
85
+ #
86
+ # Return compiled file path for the theme name
87
+
88
+ ##
89
+ # :method: views_path
90
+ #
91
+ # :call-seq: views_path(theme_name)
92
+ #
93
+ # Return views path for the theme name
94
+
95
+ [ :images, :javascripts, :stylesheets, :compiled, :views ].each do |dir|
96
+ class_eval <<-DIR_EVAL, __FILE__, __LINE__ + 1
97
+
98
+ def #{dir}_path(theme_name)
99
+ pattern = config.send :#{dir}_path
100
+ interpolate(pattern, theme_name)
101
+ end
102
+
103
+ DIR_EVAL
104
+ end
105
+
106
+ # The theme assets path is a array contains images, javascripts and stylesheets path.
107
+ def theme_assets_path(theme_name)
108
+ [ images_path(theme_name), javascripts_path(theme_name), stylesheets_path(theme_name) ]
109
+ end
110
+
111
+ # The assets path contains all themes' images, javascripts and stylesheets path.
112
+ def assets_path
113
+ Dir.glob( File.join(self.base_root, "*") ).map do |theme_name|
114
+ theme_name = File.basename(theme_name)
115
+ theme_assets_path(theme_name)
116
+ end.flatten
117
+ end
118
+
119
+ end
120
+
121
+ end
122
+
123
+ require 'theme_park/railtie' if defined?(Rails)
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ module ThemePark
3
+
4
+ class Configuration
5
+
6
+ # It is the base path for putting themes folder.
7
+ # For example in rails application is:
8
+ # ===
9
+ # Rails.root
10
+ attr_accessor :base
11
+
12
+ # It is the root path for putting each theme folder.
13
+ # usual is:
14
+ # ===
15
+ # /themes
16
+ attr_accessor :root
17
+
18
+ # The prefix is used to mount route.
19
+ attr_accessor :prefix
20
+
21
+ # The place contains images, javascripts, stylesheets, compiled files and view files.
22
+ attr_accessor :images_path, :javascripts_path, :stylesheets_path, :compiled_path, :views_path
23
+
24
+ # Handlers.
25
+ # In some scenes, we may want only one or several handlers.
26
+ attr_accessor :handlers
27
+
28
+ def initialize
29
+ # Default settings
30
+ @base = ''
31
+ @handlers = :all
32
+ @root = '/themes'
33
+ @prefix = 'themes'
34
+ @images_path = ':root/:name/assets/images'
35
+ @javascripts_path = ':root/:name/assets/javascripts'
36
+ @stylesheets_path = ':root/:name/assets/stylesheets'
37
+ @compiled_path = ':root/:name/assets/compiled'
38
+ @views_path = ':root/:name/views'
39
+ end
40
+
41
+ def base_root
42
+ File.join(base, root).chomp('/')
43
+ end
44
+
45
+ end
46
+
47
+ end