trusty-layouts-extension 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/MIT-LICENSE +98 -0
  2. data/README.md +115 -0
  3. data/Rakefile +144 -0
  4. data/VERSION +1 -0
  5. data/app/models/haml_filter.rb +5 -0
  6. data/app/models/rails_page.rb +39 -0
  7. data/app/views/layouts/trusty.html.haml +1 -0
  8. data/config/initializers/trusty_config.rb +1 -0
  9. data/config/routes.rb +0 -0
  10. data/layouts_extension.rb +21 -0
  11. data/lib/haml_layouts/models/layout.rb +33 -0
  12. data/lib/haml_layouts/models/page.rb +33 -0
  13. data/lib/layouts/engine.rb +5 -0
  14. data/lib/nested_layouts/tags/core.rb +150 -0
  15. data/lib/share_layouts/controllers/action_controller.rb +26 -0
  16. data/lib/share_layouts/helpers/action_view.rb +48 -0
  17. data/lib/tasks/layouts_extension_tasks.rake +55 -0
  18. data/lib/trusty-layouts-extension.rb +1 -0
  19. data/spec/controllers/share_controller_spec.rb +119 -0
  20. data/spec/datasets/layouts_layouts.rb +36 -0
  21. data/spec/datasets/layouts_pages.rb +43 -0
  22. data/spec/lib/haml_layouts/haml_layouts_extension_spec.rb +22 -0
  23. data/spec/lib/haml_layouts/models/layout_spec.rb +36 -0
  24. data/spec/lib/haml_layouts/models/page_spec.rb +40 -0
  25. data/spec/lib/nested_layouts/nested_layouts_extension_spec.rb +16 -0
  26. data/spec/lib/nested_layouts/tags/core_spec.rb +147 -0
  27. data/spec/lib/share_layouts/controllers/action_controller_spec.rb +44 -0
  28. data/spec/lib/share_layouts/helpers/action_view_spec.rb +171 -0
  29. data/spec/lib/share_layouts/share_layouts_extension_spec.rb +22 -0
  30. data/spec/models/haml_filter_spec.rb +0 -0
  31. data/spec/models/rails_page_spec.rb +63 -0
  32. data/spec/spec.opts +3 -0
  33. data/spec/spec_helper.rb +22 -0
  34. data/trusty-layouts-extension.gemspec +31 -0
  35. data/vendor/plugins/rails_upgrade/MIT-LICENSE +20 -0
  36. data/vendor/plugins/rails_upgrade/README.rdoc +26 -0
  37. data/vendor/plugins/rails_upgrade/Rakefile +22 -0
  38. data/vendor/plugins/rails_upgrade/init.rb +2 -0
  39. data/vendor/plugins/rails_upgrade/install.rb +38 -0
  40. data/vendor/plugins/rails_upgrade/lib/application_checker.rb +506 -0
  41. data/vendor/plugins/rails_upgrade/lib/gemfile_generator.rb +95 -0
  42. data/vendor/plugins/rails_upgrade/lib/new_configuration_generator.rb +59 -0
  43. data/vendor/plugins/rails_upgrade/lib/rails_upgrade.rb +0 -0
  44. data/vendor/plugins/rails_upgrade/lib/routes_upgrader.rb +344 -0
  45. data/vendor/plugins/rails_upgrade/lib/tasks/rails_upgrade_tasks.rake +79 -0
  46. data/vendor/plugins/rails_upgrade/test/application_checker_test.rb +344 -0
  47. data/vendor/plugins/rails_upgrade/test/gemfile_generator_test.rb +72 -0
  48. data/vendor/plugins/rails_upgrade/test/new_configuration_generator_test.rb +63 -0
  49. data/vendor/plugins/rails_upgrade/test/routes_upgrader_test.rb +218 -0
  50. data/vendor/plugins/rails_upgrade/test/test_helper.rb +5 -0
  51. data/vendor/plugins/rails_upgrade/uninstall.rb +1 -0
  52. metadata +134 -0
@@ -0,0 +1,33 @@
1
+ module HamlLayouts
2
+ module Models
3
+ module Page
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+
8
+ def parse_object(object)
9
+ # We don't want to return the haml on a layout by default
10
+ text = object.is_a?(Layout) ? object.rendered_content : object.content
11
+
12
+ if object.respond_to? :filter_id
13
+ if object.filter_id == 'Haml'
14
+ # We want to render the tags as html/radius before passing them
15
+ text = object.filter.filter(text)
16
+ text = parse(text)
17
+ else
18
+ text = parse(text)
19
+ text = object.filter.filter(text)
20
+ end
21
+ else
22
+ text = parse(text)
23
+ end
24
+ text
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,5 @@
1
+ module Layouts
2
+ class Engine < Rails::Engine
3
+ paths["app/helpers"] = []
4
+ end
5
+ end
@@ -0,0 +1,150 @@
1
+ module NestedLayouts
2
+ module Tags
3
+ module Core
4
+
5
+ include TrustyCms::Taggable
6
+
7
+ class TagError < StandardError; end
8
+
9
+ desc %{
10
+ Renders the contents of the tag inside of a "parent" layout, which is selected via the +name+
11
+ attribute. The contents of this tag are placed at a corresponding <r:content_for_layout/> tag
12
+ within the parent layout. This tag is intended to be used within your layouts, and should
13
+ only appear once per layout.
14
+
15
+ *Usage:*
16
+
17
+ <r:inside_layout name="master">
18
+ <div id="main-column">
19
+ <r:content_for_layout/>
20
+ </div>
21
+ </r:inside_layout>
22
+ }
23
+ tag 'inside_layout' do |tag|
24
+ if name = tag.attr['name']
25
+ # Prepare the stacks
26
+ tag.globals.nested_layouts_content_stack ||= []
27
+ tag.globals.nested_layouts_layout_stack ||= []
28
+
29
+ # Remember the original layout to support the +layout+ tag
30
+ tag.globals.page_original_layout ||= tag.globals.page.layout # Remember the original layout
31
+
32
+ # Find the layout
33
+ name.strip!
34
+ if layout = Layout.find_by_name(name)
35
+ # Track this layout on the stack
36
+ tag.globals.nested_layouts_layout_stack << name
37
+
38
+ # Save contents of inside_layout for later insertion
39
+ tag.globals.nested_layouts_content_stack << tag.expand
40
+
41
+ # Set the page layout that Radiant should use for rendering, which is different than the actual
42
+ # page's layout when layouts are nested. The final/highest +inside_layout+ tag will set or
43
+ # overwrite this value for the last time.
44
+ tag.globals.page.layout = layout
45
+ tag.globals.page.render
46
+ else
47
+ raise TagError.new(%{Error (nested_layouts): Parent layout "#{name.strip}" not found for "inside_layout" tag})
48
+ end
49
+ else
50
+ raise TagError.new(%{Error (nested_layouts): "inside_layout" tag must contain a "name" attribute})
51
+ end
52
+ end
53
+
54
+ desc %{
55
+ Allows nested layouts to target this layout. The contents of <r:inside_layout> tag blocks in another
56
+ layout will have their contents inserted at the location given by this tag (if they target this
57
+ layout). This tag also behaves like a standard <r:content/> tag if this layout is specified directly
58
+ by a page.
59
+
60
+ This tag is intended to be used inside layouts.
61
+
62
+ *Usage:*
63
+
64
+ <html>
65
+ <body>
66
+ <r:content_for_layout/>
67
+ </body>
68
+ </html>
69
+
70
+ }
71
+ tag 'content_for_layout' do |tag|
72
+ tag.globals.nested_layouts_content_stack ||= []
73
+
74
+ # return the saved content if any, or mimic a default +<r:content/>+ tag (render the body part)
75
+ tag.globals.nested_layouts_content_stack.pop || tag.globals.page.render_part('body')
76
+ end
77
+
78
+ desc %{
79
+ Return the layout name of the current page.
80
+
81
+ *Usage:*
82
+
83
+ <html>
84
+ <body id="<r:layout/>"
85
+ My body tag has an id corresponding to the layout I use. Sweet!
86
+ </body>
87
+ </html>
88
+ }
89
+ tag 'layout' do |tag|
90
+ current_layout_name(tag)
91
+ end
92
+
93
+ desc %{
94
+ output the contents of tag if layout equals name (support regex)
95
+
96
+ *Usage:*
97
+
98
+ <pre><code><r:if_layout name="(parent|parent_of_child)">
99
+ one of those layouts
100
+ </r:if_layout></code></pre>
101
+ }
102
+ tag 'if_layout' do |tag|
103
+ tag.expand if is_current_layout(tag)
104
+ end
105
+
106
+ desc %{
107
+ Output the contents of tag unless layout equals name (support regex)
108
+
109
+ *Usage:*
110
+
111
+ <pre><code><r:unless_layout name="parent">
112
+ not the parent layotu
113
+ </r:unless_layout></code></pre>
114
+ }
115
+ tag 'unless_layout' do |tag|
116
+ tag.expand unless is_current_layout(tag)
117
+ end
118
+
119
+ tag 'body' do |tag|
120
+ %{<body id="#{tag.locals.page.slug.gsub('/','')}" class="#{current_layout_name(tag)}#{tag.attr['class'].present? ? " #{tag.attr['class']}" : nil}">#{tag.expand}</body>}
121
+ end
122
+
123
+ protected
124
+
125
+ def current_layout_name(tag)
126
+ result = ''
127
+
128
+ if layout = tag.globals.page_original_layout
129
+ result = layout.name
130
+ elsif layout = tag.globals.page.layout
131
+ result = layout.name
132
+ end
133
+
134
+ result
135
+ end
136
+
137
+ def is_current_layout(tag)
138
+ if tag.attr['name'].nil?
139
+ raise TagError.new(%{Error (nested_layouts): "if_layout" tag must contain a "name" attribute})
140
+ end
141
+
142
+ layout = tag.globals.page_original_layout || tag.globals.page.layout
143
+ search = %r{#{tag.attr['name']}}
144
+
145
+ (layout.name =~ search).present?
146
+ end
147
+
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,26 @@
1
+ module ShareLayouts
2
+ module Controllers
3
+ module ActionController
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def trusty_layout(name=nil, options={}, &block)
11
+ raise ArgumentError, "A layout name or block is required!" unless name || block
12
+ class_attribute :trusty_layout
13
+ self.trusty_layout = name || block
14
+ before_filter :set_trusty_layout
15
+ layout 'trusty', options
16
+ end
17
+ end
18
+
19
+ def set_trusty_layout
20
+ @trusty_layout = self.class.trusty_layout
21
+ @trusty_layout = @trusty_layout.call(self) if @trusty_layout.is_a? Proc
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,48 @@
1
+ module ShareLayouts
2
+ module Helpers
3
+ module ActionView
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+
8
+ def trusty_layout(name = @trusty_layout)
9
+ page = find_page
10
+ assign_attributes!(page, name)
11
+ page.build_parts_from_hash!(extract_captures)
12
+ page.render
13
+ end
14
+
15
+ def assign_attributes!(page, name = @trusty_layout)
16
+ page.layout = Layout.where(name: name).first || page.layout
17
+ page.title = @title || @content_for_title || page.title || ''
18
+ page.breadcrumb = @breadcrumb || @content_for_breadcrumb || page.breadcrumb || page.title
19
+ page.breadcrumbs = @breadcrumbs || @content_for_breadcrumbs || nil
20
+ page.url = request.path
21
+ page.slug = page.url.split("/").last
22
+ page.published_at ||= Time.now
23
+ page.request = request
24
+ page.response = response
25
+ end
26
+
27
+ def extract_captures
28
+ @view_flow.content.inject({}) do |h, var|
29
+ key = var[0]
30
+ key = :body if key == :layout
31
+ unless key == :title || key == :breadcrumbs
32
+ h[key] = var[1]
33
+ end
34
+ h
35
+ end
36
+ end
37
+
38
+ def find_page
39
+ page = Page.find_by_url(request.path) rescue nil
40
+ page.is_a?(RailsPage) ? page : RailsPage.new(:class_name => "RailsPage")
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,55 @@
1
+ namespace :trusty_cms do
2
+ namespace :extensions do
3
+ namespace :layouts do
4
+
5
+ desc "Runs the migration of the Layouts extension"
6
+ task :migrate => :environment do
7
+ require 'trusty/extension_migrator'
8
+ if ENV["VERSION"]
9
+ LayoutsExtension.migrator.migrate(ENV["VERSION"].to_i)
10
+ Rake::Task['db:schema:dump'].invoke
11
+ else
12
+ LayoutsExtension.migrator.migrate
13
+ Rake::Task['db:schema:dump'].invoke
14
+ end
15
+ end
16
+
17
+ desc "Copies public assets of the Layouts to the instance public/ directory."
18
+ task :update => :environment do
19
+ is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
20
+ puts "Copying assets from LayoutsExtension"
21
+ Dir[LayoutsExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
22
+ path = file.sub(LayoutsExtension.root, '')
23
+ directory = File.dirname(path)
24
+ mkdir_p Rails.root + directory, :verbose => false
25
+ cp file, Rails.root + path, :verbose => false
26
+ end
27
+ unless LayoutsExtension.root.starts_with? Rails.root # don't need to copy vendored tasks
28
+ puts "Copying rake tasks from LayoutsExtension"
29
+ local_tasks_path = File.join(Rails.root, %w(lib tasks))
30
+ mkdir_p local_tasks_path, :verbose => false
31
+ Dir[File.join LayoutsExtension.root, %w(lib tasks *.rake)].each do |file|
32
+ cp file, local_tasks_path, :verbose => false
33
+ end
34
+ end
35
+ end
36
+
37
+ desc "Syncs all available translations for this ext to the English ext master"
38
+ task :sync => :environment do
39
+ # The main translation root, basically where English is kept
40
+ language_root = LayoutsExtension.root + "/config/locales"
41
+ words = TranslationSupport.get_translation_keys(language_root)
42
+
43
+ Dir["#{language_root}/*.yml"].each do |filename|
44
+ next if filename.match('_available_tags')
45
+ basename = File.basename(filename, '.yml')
46
+ puts "Syncing #{basename}"
47
+ (comments, other) = TranslationSupport.read_file(filename, basename)
48
+ words.each { |k,v| other[k] ||= words[k] } # Initializing hash variable as empty if it does not exist
49
+ other.delete_if { |k,v| !words[k] } # Remove if not defined in en.yml
50
+ TranslationSupport.write_file(filename, basename, comments, other)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1 @@
1
+ # Nothing to see here
@@ -0,0 +1,119 @@
1
+ require 'spec/spec_helper'
2
+
3
+ module TestShareLayoutsTags
4
+ include Radiant::Taggable
5
+
6
+ desc 'some tag'
7
+ tag 'test_has_request' do |tag|
8
+ tag.locals.page.request.class
9
+ end
10
+
11
+ desc 'some tag'
12
+ tag 'test_has_response' do |tag|
13
+ tag.locals.page.response.class
14
+ end
15
+
16
+ end
17
+
18
+ Page.send(:include, TestShareLayoutsTags)
19
+
20
+ class ShareController < ApplicationController
21
+ radiant_layout 'Default'
22
+
23
+ no_login_required
24
+
25
+ def normal_erb_in_radiant_layout
26
+ @name = 'Chester McTester'
27
+ render :inline => 'Hello there, <%= @name %>', :layout => true
28
+ end
29
+
30
+ def normal_erb_with_no_layout
31
+ @name = 'Chester McTester'
32
+ render :inline => 'Hello there, <%= @name %>', :layout => false
33
+ end
34
+
35
+ def normal_erb_with_different_radiant_layout
36
+ @radiant_layout = 'Different'
37
+
38
+ @name = 'Chester McTester'
39
+ render :inline => 'Hello there, <%= @name %>', :layout => true
40
+ end
41
+
42
+ def normal_erb_with_different_erb_layout
43
+ @name = 'Chester McTester'
44
+ render :inline => 'Hello there, <%= @name %>', :layout => 'application'
45
+ end
46
+
47
+ def with_request_layout
48
+ @radiant_layout = 'Test Request'
49
+
50
+ render :inline => '', :layout => true
51
+ end
52
+
53
+ def with_response_layout
54
+ @radiant_layout = 'Test Response'
55
+
56
+ render :inline => '', :layout => true
57
+ end
58
+ end
59
+
60
+ describe ShareController do
61
+ dataset :layouts
62
+
63
+ before(:each) do
64
+ @controller = ShareController.new
65
+ @request = ActionController::TestRequest.new
66
+ @response = ActionController::TestResponse.new
67
+
68
+ @erb_content = "Hello there, Chester McTester"
69
+
70
+ layout = "Content: <r:content />"
71
+ Layout.create!(:name => 'Default', :content => layout)
72
+
73
+ layout = "Different: <r:content />"
74
+ Layout.create!(:name => 'Different', :content => layout)
75
+
76
+ layout = "Request: <r:test_has_request />\nContent: <r:content />"
77
+ Layout.create!(:name => 'Test Request', :content => layout)
78
+
79
+ layout = "Response: <r:test_has_response />\nContent: <r:content />"
80
+ Layout.create!(:name => 'Test Response', :content => layout)
81
+ end
82
+
83
+ it "should render normal erb in radiant layout" do
84
+ get :normal_erb_in_radiant_layout
85
+ response.should be_success
86
+ @response.body.strip.should == "Content: #{@erb_content}"
87
+ end
88
+
89
+ it "should normal_erb_with_no_layout" do
90
+ get :normal_erb_with_no_layout
91
+ response.should be_success
92
+ @response.body.strip.should == @erb_content
93
+ end
94
+
95
+ it "should normal_erb_with_different_radiant_layout" do
96
+ get :normal_erb_with_different_radiant_layout
97
+ response.should be_success
98
+ @response.body.strip.should == "Different: #{@erb_content}"
99
+ end
100
+
101
+ it "should normal_erb_with_different_erb_layout" do
102
+ get :normal_erb_with_different_erb_layout
103
+ response.should be_success
104
+ @response.body.should =~ /#{@erb_content}/
105
+ end
106
+
107
+ it "should should_assign_request" do
108
+ get :with_request_layout
109
+ response.should be_success
110
+ @response.body.should =~ /Request: .*Request$/
111
+ end
112
+
113
+ it "should should_assign_response" do
114
+ get :with_response_layout
115
+ response.should be_success
116
+ @response.body.should =~ /Response: .*Response$/
117
+ end
118
+
119
+ end
@@ -0,0 +1,36 @@
1
+ class LayoutsLayoutsDataset < Dataset::Base
2
+
3
+ def load
4
+ create_record :layout, :parent,
5
+ :name => 'parent',
6
+ :content_type => 'haml',
7
+ :content => <<-CONTENT
8
+ !!! 5
9
+ %html
10
+ %head
11
+ %title Title
12
+ :plain
13
+ <r:body class="site">
14
+ <r:content_for_layout />
15
+ </r:body>
16
+ CONTENT
17
+
18
+ create_record :layout, :child,
19
+ :name => 'child',
20
+ :content => <<-CONTENT
21
+ <r:inside_layout name='parent'>
22
+ <h1><r:layout/></h1>
23
+ </r:inside_layout>
24
+ CONTENT
25
+
26
+ create_record :layout, :haml,
27
+ :name => 'haml',
28
+ :content_type => 'haml',
29
+ :content => <<-CONTENT
30
+ %r:inside_layout{:name=>"parent"}
31
+ %h1{:id=>"<r:layout />",:'data-layout'=>"<r:find url='find'><r:layout/></r:find>"}
32
+ %r:layout
33
+ CONTENT
34
+ end
35
+
36
+ end