Skip to content

Commit

Permalink
refactored some dup code in component, cleaned routing example
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Sam-Bodden committed Jan 30, 2010
1 parent d0050f7 commit c658779
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
25 changes: 11 additions & 14 deletions examples/routing/source/routing.rb
Expand Up @@ -24,25 +24,22 @@ class RoutedDifferentlyWithParams < Trellis::Page
route '/day_of_the_year/:year/:month/:day'

def parse_date
Date.parse("#{@month}/#{@day}/#{@year}")
Date.parse("#{@month}/#{@day}/#{@year}").strftime("%e %B, %Y")
end

def on_select
self
end

template do
thtml {
body {
h2 {
text %[Date <trellis:value name="page.parse_date"/>]
}
text %[is the <trellis:eval expression="Date.parse(month + '/' + day + '/' + year).yday.to_s"/> day of the year]
br
text %[<trellis:action_link>Refresh</trellis:action_link>]
}
}
end

template %[
<html xml:lang="en" lang="en"
xmlns:trellis="http://trellisframework.org/schema/trellis_1_0_0.xsd"
xmlns="http://www.w3.org/1999/xhtml">
<h2>@{parse_date}@ is the @{Date.parse(@month + '/' + @day + '/' + @year).yday.ordinalize.to_s}@ day of the year!</h2>
<br/>
<trellis:action_link>Refresh</trellis:action_link>
</html>
], :format => :eruby
end

Routing.new.start if __FILE__ == $PROGRAM_NAME
Expand Down
31 changes: 25 additions & 6 deletions lib/trellis/trellis.rb
Expand Up @@ -817,15 +817,14 @@ def self.find_components

def self.process_component_contributions(component, classes_processed, attributes=nil)
unless classes_processed.include?(component)
component.add_style_links_to_page(self, attributes)
component.add_script_links_to_page(self, attributes)
component.add_class_styles_to_page(self, attributes)
component.add_class_scripts_to_page(self, attributes)
[:style_links, :script_links, :class_styles, :class_scripts].each do |what|
component.add_to_page(self, what, attributes)
end
component.add_document_modifications_to_page(self)
end

component.add_styles_to_page(self, attributes)
component.add_scripts_to_page(self, attributes)
component.add_to_page(self, :styles, attributes)
component.add_to_page(self, :scripts, attributes)

classes_processed << component unless classes_processed.include?(component)
end
Expand Down Expand Up @@ -1065,6 +1064,26 @@ def self.field(sym, options=nil)
send("#{sym}=", default_value) if default_value
end

def self.add_to_page(page, what, attributes)
builder = Builder::XmlMarkup.new
collection = self.send(what)
location = [:class_scrips, :scripts].include?(what) ? "html/body" : "html/head"
collection.each do |element|
element = element.replace_ant_style_properties(attributes) if attributes
case what
when :style_links
value = builder.link(:rel => "stylesheet", :type => "text/css", :href => element)
when :script_links
value = builder.script('', :type => "text/javascript", :src => element)
when :class_styles, :styles
value = builder.style(:type => "text/css") { |builder| builder << element }
when :class_scripts, :scripts
value = builder.script(:type => "text/javascript") { |builder| builder << element }
end
page.dom.at_css(location).children.last.after("\n#{value}")
end
end

def self.add_style_links_to_page(page, attributes)
style_links.each do |href|
href = href.replace_ant_style_properties(attributes) if attributes
Expand Down

0 comments on commit c658779

Please sign in to comment.