<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Carbon Emitter &#187; prototyping</title>
	<atom:link href="http://blog.carbonfive.com/tag/prototyping/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.carbonfive.com</link>
	<description>The blog of Carbon Five</description>
	<lastBuildDate>Thu, 19 Apr 2012 16:48:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Ew&#8230; you got CSS in my Javascript</title>
		<link>http://blog.carbonfive.com/2010/10/21/ew-you-got-css-in-my-javascript/</link>
		<comments>http://blog.carbonfive.com/2010/10/21/ew-you-got-css-in-my-javascript/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 23:29:32 +0000</pubDate>
		<dc:creator>Andy Peterson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[prototyping]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=1328</guid>
		<description><![CDATA[The other weekend I test drove a little Javascript library to output CSS style rules from within Javascript. I took the most obvious Javascript-literal approach to get the most out of Javascript support in editors. I called it Csster (&#8220;sister&#8221;), &#8230; <a href="http://blog.carbonfive.com/2010/10/21/ew-you-got-css-in-my-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The other weekend I test drove a<a href="http://github.com/ndp/csster"> little Javascript library</a> to output CSS style rules from within Javascript. I took the most obvious Javascript-literal approach to get the most out of Javascript support in editors. I called it Csster (&#8220;sister&#8221;), and it looks like:<br />
<code>Csster.style({<br />
    h1: { fontSize: 18, color: 'chartreuse'    }<br />
});</code><br />
All it does is insert the rule into the DOM so that H1 elements are, well, <span style="font-size:18px;color:chartreuse;">chartreuse</span>. There wasn&#8217;t initially much code behind this, but I thought it might be useful. It&#8217;s pretty much the same as <a href="http://api.jquery.com/css/">jQuery&#8217;s css function</a>, but writes out a rule instead of immediately applying it to all the elements.</p>
<p>I immediately added nesting of rules, similar to how SASS lets you dry-up your stylesheets. Then, with a little help from Alex, I added basic color conversions and functions like &#8220;darken&#8221; and &#8220;saturate&#8221;<a href="http://ndpsoftware.com/csster/demo.html">(demo here)</a>. I then added <a href="http://www.paulgraham.com/avg.html">support for macros</a>, so you could mix-in behaviors like rounded corners, drop shadows, and of course the ubiquitous (and awkwardly named) &#8220;clearfix&#8221;. My last pass was to make sure there are reasonable extension points for other developers. Now you can write concise and expressive CSS right along with your Javascript.</p>
<p>Since it&#8217;s POJS (Plain Old Javascript), it is a compact language. There&#8217;s was no need for me to write math or looping or logic functions, since Javascript&#8217;s already got them (unlike SASS). DSLs are the hottest things these days, but even the not-so-forgiving syntax of Javascript works pretty well in this case.</p>
<p>Why would you want to use this? Well, I thought that was the big question, but everyone I show it to has an answer. For me, I am writing pages that generate lots of their HTML markup anyway, so it is awkward to keep a separate CSS file in sync (and drag it down from the server). It works well to write self-contained jQuery plugins, as I <a href="http://ndpsoftware.com/csster/demo_chart.html">sketched out here</a>. I started using on a new initiative (that I can&#8217;t share yet) of <a href="http://bedsider.org/methods">my latest project</a>, and it made prototyping and initial build-out much faster. It also makes using sprites painless&#8211; all our subtitles are in one sprite and I simply add a <code>has: subtitle("Emotion")</code> as one of the attributes and it works. (Previously I&#8217;d written some Ruby code to generate a CSS. Ughh.)</p>
<p>Please, <a href="http://github.com/ndp/csster">give it a spin</a>, and file bugs or send me comments on github.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/10/21/ew-you-got-css-in-my-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convenient CSS and Javascript in Ruby on Rails</title>
		<link>http://blog.carbonfive.com/2009/03/08/convenient-css-and-javascript-in-ruby-on-rails/</link>
		<comments>http://blog.carbonfive.com/2009/03/08/convenient-css-and-javascript-in-ruby-on-rails/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 17:41:11 +0000</pubDate>
		<dc:creator>Andy Peterson</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[prototyping]]></category>
		<category><![CDATA[rapid development]]></category>
		<category><![CDATA[Software Design]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=480</guid>
		<description><![CDATA[I get tired of hunting through a hierarchy of folders and files, from the views to the public folder, to locate a certain CSS or Javascript file. It&#8217;d be convenient to have them right with the markup, but embedding these &#8230; <a href="http://blog.carbonfive.com/2009/03/08/convenient-css-and-javascript-in-ruby-on-rails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I get tired of hunting through a hierarchy of folders and files, from the views to the public folder, to locate a certain CSS or Javascript file. It&#8217;d be convenient to have them right with the markup, but embedding these definitions within your HTML markup is a bad idea for several reasons. For our current project, I proposed we put everything in the view directory so they are easy to find:</p>
<pre class="brush: java; light: true; title: ; wrap-lines: false; notranslate">app/
  views/
    home/
      index.html.rb
      index.css
      index.js</pre>
<p>The convention is clear: to add page-specific CSS code, just create a new file with the same name as the view, in the same folder. Easy to add, edit and remove. The alternative (using the public folder), usually leads to a parallel hierarchy and the inconvenience of that.</p>
<p>I also thought, that this being Rails and all, the files should be included automatically. It turned out to be pretty easy.<span id="more-480"></span></p>
<h2>The Implementation</h2>
<h3>Serving the Assets</h3>
<p>To serve this file, we need a route and a controller.</p>
<p>First the route:</p>
<pre class="brush: ruby; light: true; title: ; wrap-lines: false; notranslate">map.connect '/asset/:path',
    :controller =&amp;gt; 'asset', :action =&amp;gt; 'serve_asset', :path =&amp;gt; /.*.(js|css)/</pre>
<p>We need to be careful here, since we&#8217;re going to be serving files directly out of our view hierarchy&#8211; make sure the route only picks up the javascript and CSS files.</p>
<p>The corresponding controller:</p>
<pre class="brush: ruby; light: true; title: ; wrap-lines: false; notranslate">class AssetController &amp;lt; ApplicationController
  def serve_asset
    path = params[:path]
    format = path.sub(/.*.(w+$)/, '1')
    respond_to do |format|
      format.js { render :file=&amp;gt;&quot;#{RAILS_ROOT}/app/views/#{path}&quot;}
      format.css { render(:file=&amp;gt;&quot;#{RAILS_ROOT}/app/views/#{path}&quot;)}
    end
  end</pre>
<p>Now the assets mentioned above are served as /assets/index.css and /assets/index.js.</p>
<h3>Including the Assets</h3>
<p>With this convention, we added code to our layout that includes these files automatically, but only if they exist. First, the additional controller code:</p>
<pre class="brush: ruby; light: true; title: ; wrap-lines: false; notranslate">class AssetController &amp;lt; ApplicationController
  ...
  def self.include_asset(rel_path)
    return '' unless AssetController.asset_exists?(rel_path)
    case rel_path
      when /.js$/
        &quot;&lt;!--mce:0--&gt;&quot;
      when /.css$/
        &quot;
&quot;
    end
  end

  def self.asset_exists?(rel_path)
    FileTest.exists?(RAILS_ROOT + &quot;/app/views/#{rel_path}&quot;)
  end</pre>
<p>And the code needed in the (Erector) layout:</p>
<pre class="brush: ruby; light: true; title: ; wrap-lines: false; notranslate">head do
  c = self.class
  rawtext AssetController.include_asset(&quot;#{c.name.underscore}.js&quot;)
  rawtext AssetController.include_asset(&quot;#{c.name.underscore}.css&quot;)
end</pre>
<p>Actually, since it&#8217;s Erector, the views are Ruby classes, so there is a hierarchy. These assets may be specific to any one of these:</p>
<pre class="brush: ruby; light: true; title: ; wrap-lines: false; notranslate">c = self.class
while c != Object
  rawtext AssetController.include_asset(&quot;#{c.name.underscore}.js&quot;)
  rawtext AssetController.include_asset(&quot;#{c.name.underscore}.css&quot;)
  c = c.superclass
end</pre>
<h2>Discussion</h2>
<p>Although some people will bristle at the proliferation of files, I have used this technique repeatedly over the last five years (in Java and PHP projects). I find it leads to sound, modular development. With Ruby, it&#8217;ll be pretty straightforward to concat the assets together later on, to reduce requests, or even embed the code directly in the served HTML files, if it&#8217;s small enough. It&#8217;s also convenient to add page caching in the controller, when the time comes. Adding</p>
<pre class="brush: ruby; light: true; title: ; wrap-lines: false; notranslate">  caches_page(:serve_asset)</pre>
<p>copies all these files into public/assets (as they are accessed), which conveniently takes your mongrel (or whatever) out of the picture.</p>
<p>You might imagine this keeps you from organizing and structuring your CSS and Javascript; it doesn&#8217;t do that. It lowers the barier to using a &#8220;local&#8221; scope for the CSS and Javascript. It allows you to write page-specific CSS and Javascript, which if you&#8217;re developing code quickly, is a lifesaver. We have repeatedly opened a view-specific Javascript file, seen that it no longer applies to the code, and removed it&#8211; safely. It&#8217;s easy to edit scripts when you know they only effect one page. Without this, I tend to lump things together that don&#8217;t really belong, and it becomes hard to tease them apart later.</p>
<p>Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/03/08/convenient-css-and-javascript-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

