altered_views 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in altered_views.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/Readme.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = altered_views
2
+
3
+ == Synopsis
4
+ Everybody is accustomed now to the power and flexibility inheritance gives you in OOP. But when it comes to view life is getting really complex! Let me give you an example.
5
+
6
+ You have a nice rails engine that provides you with authentication functionality you use throughout your applications. But the login page should really look slightly different in this app and that registration page should carry different logo, name and copyright. The only option you have is to copy those views into the app and redo them. That is it, from this point you've lost it, cause any bugs you fixed in the views inside the engine still live in the copied views.
7
+
8
+ This is where altered_views comes to help. It allows you to inherit the engine's view and alter only the bits you need referring to them using css selectors.
9
+
10
+ You view in the *engine* could look like this:
11
+
12
+ app/views/user_session/new.html.erb [engine]
13
+
14
+ <h1 id="login-title">Login:</h1>
15
+ <div class="form-container">
16
+ <!-- Code to display login box, which I omit -->
17
+ </div>
18
+ <span class="legal">Some legal nonsence</span>
19
+
20
+ Now we will alter customizing only required parts. This view goes inside your *application*.
21
+
22
+ app/views/user_session/new.html.erb [application]
23
+
24
+ <%= inherit_view "parent:admin/cataloguer/categories/edit" do %>
25
+ <%= content "#login-title" do %>
26
+ Login into my company's account:
27
+ <% end %>
28
+
29
+ <%= append ".legal" do %>
30
+ <p>Exta clause</p>
31
+ <% end %>
32
+ <% end %>
33
+
34
+
35
+ altered_views support the following DOM modifiers:
36
+
37
+ * content — replace the element's content
38
+ * before — place content before the node
39
+ * after — place content after the node
40
+ * append — content in appended in the end of the element
41
+ * prepend — content in placed as the first child of the element before other content
42
+
43
+ == Installing
44
+ altered_views is available on gemcutter so all you have to do it
45
+ gem install altered_views
46
+
47
+ Then include it in your Gemfile
48
+ gem altered_views
49
+
50
+ == Requirements
51
+ altered_views was written to extend Rails 3.0 functionality and uses nokogiri — a fast and powerful XML/HTML processing engine.
52
+
53
+ == Known issues
54
+ This is the very first version of the gem and might contain bugs.
55
+
56
+ * it assumes you are using layouts in your application. If your view contains the whole page — it will cause problems.
57
+ * head section modification is not supported
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/altered_views/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "altered_views"
6
+ s.version = AltereddViews::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.date = %q{2010-12-24}
9
+ s.authors = ["Sergey Kuleshov"]
10
+ s.email = ["svyatogor@gmail.com"]
11
+ # s.homepage = "http://rubygems.org/gems/altered_views"
12
+ s.summary = "Inherit and extend rails views in OOP manner"
13
+ s.description = "Altered views allows one to inherit views from pluged in engines and extend them by operating on the DOM model."
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+ s.rubyforge_project = "altered_views"
17
+
18
+ s.add_runtime_dependency "rails", "~> 3.0.0"
19
+ s.add_runtime_dependency "nokogiri", "~> 1.4"
20
+ s.add_development_dependency "bundler", ">= 1.0.0"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
24
+ s.require_path = 'lib'
25
+
26
+ s.extra_rdoc_files = [
27
+ "Readme.rdoc"
28
+ ]
29
+ end
@@ -0,0 +1,23 @@
1
+ module ActionView
2
+ class PathSet
3
+ def find_all(*args)
4
+ @cache ||= {}
5
+ call_parent = false
6
+
7
+ if args[1].is_a? String and args[1] =~ /^parent:/
8
+ call_parent = true
9
+ args[1] = args[1].gsub(/^parent:/, '')
10
+ end
11
+
12
+ if @cache.has_key? args.hash and call_parent
13
+ list = @cache[args.hash]
14
+ else
15
+ list = map do |resolver|
16
+ templates = resolver.find_all(*args)
17
+ end.reject {|templates| templates.empty? }
18
+ @cache[args.hash] = list
19
+ end
20
+ list.empty? ? [] : list.shift
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module AltereddViews
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require "altered_views/path_set_extension"
2
+
3
+ module AltereddViews
4
+ def inherit_view(name, &block)
5
+ content = capture {render(:template => name) }
6
+ @doc = Nokogiri::HTML content
7
+ yield
8
+ raw @doc.css("body > *").to_s
9
+ end
10
+
11
+ private
12
+ def content(selector, &block)
13
+ find_node(selector).each {|node| node.inner_html = capture(&block)}
14
+ ""
15
+ end
16
+
17
+ def before(selector, &block)
18
+ find_node(selector).each {|node| node.before capture(&block)}
19
+ ""
20
+ end
21
+
22
+ def after(selector, &block)
23
+ find_node(selector).each {|node| node.after capture(&block)}
24
+ ""
25
+ end
26
+
27
+ def append(selector, &block)
28
+ find_node(selector).each {|node| node.add_child capture(&block)}
29
+ ""
30
+ end
31
+
32
+ def prepend(selector, &block)
33
+ find_node(selector).each do |node|
34
+ c = node.children;
35
+ node.inner_html = capture(&block); node.add_child c
36
+ end
37
+ ""
38
+ end
39
+
40
+ def find_node(selector)
41
+ els = @doc.css selector
42
+ if els.empty?
43
+ raise "Selector \"#{selector}\" not found in the view."
44
+ else
45
+ return els
46
+ end
47
+ end
48
+ end
49
+
50
+ ActionController::Base.send :add_template_helper, AltereddViews
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: altered_views
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Sergey Kuleshov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-24 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: nokogiri
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 1
48
+ - 4
49
+ version: "1.4"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: bundler
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: Altered views allows one to inherit views from pluged in engines and extend them by operating on the DOM model.
69
+ email:
70
+ - svyatogor@gmail.com
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - Readme.rdoc
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - Rakefile
81
+ - Readme.rdoc
82
+ - altered_views.gemspec
83
+ - lib/altered_views.rb
84
+ - lib/altered_views/path_set_extension.rb
85
+ - lib/altered_views/version.rb
86
+ has_rdoc: true
87
+ homepage:
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 23
110
+ segments:
111
+ - 1
112
+ - 3
113
+ - 6
114
+ version: 1.3.6
115
+ requirements: []
116
+
117
+ rubyforge_project: altered_views
118
+ rubygems_version: 1.3.7
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Inherit and extend rails views in OOP manner
122
+ test_files: []
123
+