Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First actual import of code from Active Admin
- Loading branch information
Showing
26 changed files
with
1,543 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,34 @@ | ||
Arbre - Coming Soon. For now, checkout Active Admin: | ||
= Arbre - Ruby Object Oriented HTML Views | ||
|
||
http://github.com/gregbell/active_admin | ||
Arbre is the DOM implemented in Ruby. This project is primarily used as | ||
the object oriented view layer in Active Admin. | ||
|
||
== Simple Usage | ||
|
||
A simple example of setting up an Arbre context and creating some html | ||
|
||
html = Arbre::Context.new do | ||
h2 "Why Arbre is awesome?" | ||
|
||
ul do | ||
li "The DOM is implemented in ruby" | ||
li "You can create object oriented views" | ||
li "Templates suck" | ||
end | ||
end | ||
|
||
puts html.to_s #=> <h2>Why</h2><ul><li></li></ul> | ||
|
||
|
||
== The DOM in Ruby | ||
|
||
The purpose of Arbre is to leave the view as ruby objects as long | ||
as possible. This allows OO Design to be used to implement the view layer. | ||
|
||
|
||
html = Arbre::Context.new do | ||
h2 "Why Arbre is awesome?" | ||
end | ||
|
||
html.children.size #=> 1 | ||
html.children.first #=> #<Arbre::HTML::H2> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
require 'active_support/core_ext/string/output_safety' | ||
require 'active_support/inflector' | ||
|
||
module Arbre | ||
end | ||
|
||
require 'arbre/element' | ||
require 'arbre/context' | ||
require 'arbre/html/attributes' | ||
require 'arbre/html/class_list' | ||
require 'arbre/html/tag' | ||
require 'arbre/html/text_node' | ||
require 'arbre/html/document' | ||
require 'arbre/html/html5_elements' | ||
require 'arbre/component' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module Arbre | ||
|
||
# Include this module in any context to start building html. | ||
# | ||
# assigns = {} | ||
# include Arbre::Builder | ||
# span("foo").to_s #=> "<span>foo</span> | ||
# | ||
# When you include the module, you are required to setup 2 variables: | ||
# | ||
# * assigns: This is a hash that includes local variables | ||
# * helpers: This is an object that provides helper methods to all | ||
# objects within the context. | ||
module Builder | ||
|
||
module BuilderMethods | ||
def build_tag(klass, *args, &block) | ||
tag = klass.new(assigns, helpers) | ||
tag.parent = current_dom_context | ||
|
||
# If you passed in a block and want the object | ||
if block_given? && block.arity > 0 | ||
# Set out context to the tag, and pass responsibility to the tag | ||
with_current_dom_context tag do | ||
tag.build(*args, &block) | ||
end | ||
else | ||
# Build the tag | ||
tag.build(*args) | ||
|
||
# Render the blocks contents | ||
if block_given? | ||
with_current_dom_context tag do | ||
append_return_block(yield) | ||
end | ||
end | ||
end | ||
|
||
tag | ||
end | ||
|
||
def insert_tag(klass, *args, &block) | ||
tag = build_tag(klass, *args, &block) | ||
current_dom_context.add_child(tag) | ||
tag | ||
end | ||
|
||
def current_dom_context | ||
@__current_dom_element_buffer__ ||= [self] | ||
current_element = @__current_dom_element_buffer__.last | ||
if current_element == self | ||
self | ||
else | ||
current_element.current_dom_context | ||
end | ||
end | ||
|
||
def with_current_dom_context(tag) | ||
raise ArgumentError, "Can't be in the context of nil. #{@__current_dom_element_buffer__.inspect}" unless tag | ||
current_dom_context # Ensure a context is setup | ||
@__current_dom_element_buffer__.push tag | ||
yield | ||
@__current_dom_element_buffer__.pop | ||
end | ||
alias_method :within, :with_current_dom_context | ||
|
||
# Appends the value to the current DOM element if there are no | ||
# existing DOM Children and it responds to #to_s | ||
def append_return_block(tag) | ||
return nil if current_dom_context.children? | ||
|
||
if !tag.is_a?(Arbre::Element) && tag.respond_to?(:to_s) | ||
current_dom_context << Arbre::HTML::TextNode.from_string(tag.to_s) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Arbre | ||
class Component < Arbre::HTML::Div | ||
|
||
# By default components render a div | ||
def tag_name | ||
'div' | ||
end | ||
|
||
def initialize(*) | ||
super | ||
add_class default_class_name | ||
end | ||
|
||
protected | ||
|
||
# By default, add a css class named after the ruby class | ||
def default_class_name | ||
self.class.name.demodulize.underscore | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'arbre/element' | ||
|
||
module Arbre | ||
class Context < Element | ||
|
||
def initialize(assigns = {}, helpers = nil, &block) | ||
assigns.symbolize_keys! | ||
super(assigns, helpers) | ||
instance_eval &block | ||
end | ||
|
||
def indent_level | ||
# A context does not increment the indent_level | ||
super - 1 | ||
end | ||
|
||
def bytesize | ||
cached_html.bytesize | ||
end | ||
alias :length :bytesize | ||
|
||
def respond_to?(method) | ||
super || cached_html.respond_to?(method) | ||
end | ||
|
||
# Webservers treat Arbre::Context as a string. We override | ||
# method_missing to delegate to the string representation | ||
# of the html. | ||
def method_missing(method, *args, &block) | ||
if cached_html.respond_to? method | ||
cached_html.send method, *args, &block | ||
else | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
# Caches the rendered HTML so that we don't re-render just to | ||
# get the content lenght or to delegate a method to the HTML | ||
def cached_html | ||
if defined?(@cached_html) | ||
@cached_html | ||
else | ||
html = to_s | ||
@cached_html = html if html.length > 0 | ||
html | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.