<?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>David Lowry &#187; Code</title>
	<atom:link href="http://www.davidlowry.co.uk/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidlowry.co.uk</link>
	<description></description>
	<lastBuildDate>Sat, 05 Nov 2011 14:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActiveAdmin on Heroku (Rails 3.1)</title>
		<link>http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/</link>
		<comments>http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 09:00:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.davidlowry.co.uk/?p=400</guid>
		<description><![CDATA[Just a brief note on getting the new ActiveAdmin gem working with Heroku under Rails 3.1 (on Thin and probably some other servers too) ActiveAdmin looks like a really useful tool to avoid Scaffold-itis when it comes to admin interfacing in Rails apps. You install the gem and loosely define what models you&#8217;re using; and you...<a href="http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Just a brief note on getting the new <a href="http://activeadmin.info/">ActiveAdmin</a> gem working with Heroku under Rails 3.1 (on Thin and probably some other servers too)</p>
<p>ActiveAdmin looks like a really useful tool to avoid Scaffold-itis when it comes to admin interfacing in Rails apps. You <a href="http://activeadmin.info/documentation.html">install the gem and loosely define</a> what models you&#8217;re using; and you can create simple effective dashboards.</p>
<p>It works easily on local testing, but when you deploy to production on Heroku (or locally with thin <code>$ bundle exec rails server thin -e production</code> the build will fail citing <code>/Users/dave/.rvm/gems/ruby-1.9.2-p290/gems/sass-rails-3.1.0/lib/sass/rails/railtie.rb:38:in `block in ': uninitialized constant Sass::Rails::SassTemplate (NameError)</code>&#8230; or similar.</p>
<p>By default the SASS gem is defined within &#8216;assets&#8217; in the Gemfile.  Move the line</p>
<p><code><br />
group :assets do<br />
gem 'sass-rails', " ~&gt; 3.1.0"<br />
..<br />
end<br />
</code></p>
<p>outside the assets group, and it will spin up without a problem. <a href="https://github.com/rails/sass-rails/issues/38">It seems the assets group</a> is processed after other gems.</p>
<p>Solution found via this post: <a href="https://github.com/rails/sass-rails/issues/38">https://github.com/rails/sass-rails/issues/38</a><br />
Great <a href="http://railscasts.com/episodes/284-active-admin">introductory ActiveAdmin tutorial at Railscasts</a></p>
<p>Now to actually build the dashboard&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/400/activeadmin-on-heroku-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Blog Move 1: Getting WordPress data to Ruby using XML</title>
		<link>http://www.davidlowry.co.uk/341/export-wordpress-to-ruby-rails/</link>
		<comments>http://www.davidlowry.co.uk/341/export-wordpress-to-ruby-rails/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 22:44:50 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=341</guid>
		<description><![CDATA[Step 1 in the &#8220;Moving my blog&#8221; process is &#8220;Extract the current site’s data into a manageable format&#8221; Frankly, that&#8217;s easy! WordPress has a functionality to export the site&#8217;s content to a single XML file containing all the published Categories, Tags, Posts, Pages and Comments. To do this (WordPress v2.9.2) click Tools &#62; Export and...<a href="http://www.davidlowry.co.uk/341/export-wordpress-to-ruby-rails/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Step 1 in the &#8220;Moving my blog&#8221; process is &#8220;Extract the current site’s data into a manageable format&#8221;</p>
<p>Frankly, that&#8217;s easy! WordPress has a functionality to export the site&#8217;s content to a single XML file containing all the published Categories, Tags, Posts, Pages and Comments. To do this (WordPress v2.9.2) click Tools &gt; Export and save the file. In previous versions of the software I believe it&#8217;s under the Manage menu.</p>
<p><script src="http://gist.github.com/427052.js?file=gistfile4.xml"></script><br />
 <noscript><a href="http://davidlowry.co.uk/wordpress/wp-content/uploads/2010/06/Screen-shot-2010-06-05-at-22.43.57.png"><img class="alignnone size-full wp-image-342" title="Wordpress XML Code" src="http://davidlowry.co.uk/wordpress/wp-content/uploads/2010/06/Screen-shot-2010-06-05-at-22.43.57.png" alt="Wordpress XML Code: viewed in Dashcode, if you care." width="613" height="323" /></a></noscript></p>
<p>I&#8217;m aware I could import the data directly from the WordPress database (to wherever it goes in the end) but let&#8217;s imagine we can&#8217;t. Anyway, database access would be tediously slow and inefficient to test against and implement.</p>
<p>A quick google for <a href="http://www.google.co.uk/search?q=%22import+wordpress+xml+ruby%22">&#8220;import wordpress xml ruby&#8221;</a> threw up nothing helpful so I turned to the Ruby XML libraries. <a href="http://railstips.org/">John Nunemaker</a> &#8220;feverishly posts everything he learns&#8221; at <a href="http://railstips.org/">railstips.org</a> and has two articles of use here:</p>
<ul>
<li><a href="http://railstips.org/blog/archives/2006/12/09/parsing-xml-with-hpricot/">Parsing XML with hpricot</a></li>
<li><a href="http://railstips.org/blog/archives/2008/08/11/parsing-xml-with-ruby/">Parsing XML with Ruby</a></li>
</ul>
<p>The latter deals with three different ruby xml libraries and compares their speed, ease of use and how nice their names are to say. He puts <a href="http://www.germane-software.com/software/rexml/">REXML</a>, <a href="http://hpricot.com/">hpricot</a> and <a href="http://libxml.rubyforge.org/">libxml-ruby</a>. I&#8217;ll save you the pleasure of reading the article (if you like) and ccv John&#8217;s summary:</p>
<blockquote><p>
&#8220;Libxml is blisteringly fast, [but] Hpricot has cooler name, REXML and Hpricot both feel easier to use out of the box&#8221;
</p></blockquote>
<p>And there you go. Hpricot it is!</p>
<p>Now to get the data into Ruby. After a quick glance at the rubytips article and <a href="http://rdoc.info/projects/hpricot/hpricot">The RDocs</a> I put together this code as a starting point:<br />
 <script src="http://gist.github.com/427052.js?file=gistfile2.builder"></script><br />
<noscript></p>
<pre><code>
cats_hierarchy={}
(doc/"wp:category").each do |category|
    cat_name = category.at("wp:category_nicename").innerHTML
    cat_parent = category.at("wp:category_parent").innerHTML

    if cats_hierarchy.include? cat_parent
        cats_hierarchy[cat_parent] = cat_name
    else
        cats_hierarchy[cat_name] = []
    end
end

cats = cats_hierarchy.to_a.flatten
</code></pre>
<p></noscript></p>
<p>That gives me two each to use Ruby objects each containing all of my category data: a hash which preserves the hierarchy of the structure and all the names in a linear array.<br />
<script src="http://gist.github.com/427052.js?file=gistfile3.builder"></script><br />
<noscript></p>
<pre><code>
?> cats = cats_hierarchy.to_a.flatten.uniq
=> ["route66", nil, "rails", "american-2008", "reciprocal-affection", "hope-for-the-future", "code", "blog", "review-blog", "rant", "brands", "projects", "yab_shop", "textpattern", "meaningful-labor", "giants", "accessibility", "root", "charity-project", "apple", "xhtml", "america-2006-route-66", "ruby", "learning", "america-2007", "uncategorized", "iphone", "america-2008"]

?> cats_hierarchy
=> {"route66"=>nil, "rails"=>nil, "american-2008"=>nil, "reciprocal-affection"=>nil, "hope-for-the-future"=>nil, "code"=>nil, "blog"=>"review-blog", "rant"=>nil, "brands"=>nil, "projects"=>nil, "yab_shop"=>nil, "textpattern"=>nil, "meaningful-labor"=>nil, "giants"=>nil, "accessibility"=>nil, "root"=>nil, "charity-project"=>nil, "apple"=>nil, "xhtml"=>nil, "america-2006-route-66"=>nil, "ruby"=>nil, "learning"=>nil, "america-2007"=>nil, "uncategorized"=>nil, "iphone"=>nil, "america-2008"=>nil}
</code></pre>
<p></noscript></p>
<p>And so we have the starting point to getting this WordPress exported XML data into a Ruby application.</p>
<p>More soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/341/export-wordpress-to-ruby-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails: Get some random records</title>
		<link>http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/</link>
		<comments>http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 13:04:38 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=293</guid>
		<description><![CDATA[EDIT AGAIN: Much better.. To get a &#8216;num_reqd&#8217; array of random objects, you can use something like this. named_scope :large, :conditions => ['image_file_name IS NOT ?', nil] named_scope :small, :conditions => ['small_image_file_name IS NOT ?', nil] def self.get(num_reqd,features_arr=[],size="large") if size=="small" collection = Feature.small elsif size=="large" collection = Feature.large end return collection if collection.size num_reqd, rder...<a href="http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>EDIT AGAIN: Much better..</p>
<p>To get a &#8216;num_reqd&#8217; array of random objects, you can use something like this.</p>
<pre><code>  named_scope :large, :conditions => ['image_file_name IS NOT ?', nil]
  named_scope :small, :conditions => ['small_image_file_name IS NOT ?', nil] 

  def self.get(num_reqd,features_arr=[],size="large")
    if size=="small"
      collection = Feature.small
    elsif size=="large"
      collection = Feature.large
    end

    return collection if collection.size <= num_reqd

    # num_reqd.times{feature=self.random(collection); features_arr.push(feature) unless features_arr.include?(feature)}
    features_arr = collection.find(:all, :limit => num_reqd, <img src='http://www.davidlowry.co.uk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => 'rand()')

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr, size)
    else
      return features_arr
    end
  end</code></pre>
<p>EDIT: It's much cleaner and easier to use something in the form below, though the following is probably useful in some cases and is possibly interesting as a code snippet.<br />
<code>
<pre>
User.find(:all, <img src='http://www.davidlowry.co.uk/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder => 'rand()')
</pre>
<p></code><br />
---- end edit.</p>
<p>Working from a baseline of the code found <a href="http://almosteffortless.com/2007/12/04/random-records-in-rails/">here at almosteffortless.com</a> I've extended a 'random record grabber' to get a specific number of unique records from a Rails data table.</p>
<p>Basically - the random method makes a database call to get the ids of a table, and sends back a random entry. <code>self.get</code> is a recursive method which provides a 'total number required' and a base array to start from (if you wish to specify entries to appear in the otherwise 'random' list). First year computer science should help get your head around the rest!</p>
<p><code>
<pre>
def self.random
    ids = connection.select_all("SELECT id FROM features")
    find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
  end

  def self.get(num_reqd,features_arr=[])
    num_reqd.times{feature=self.random; features_arr.push(feature) unless features_arr.include?(feature)}

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr)
    else
      return features_arr
    end
  end</pre>
<p></code></p>
<p>Be aware, there is more efficiency to be found in the database call (i.e. it should be cached). Also, you'll want to be sure there are at least 'num_reqd' items in the database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/293/ruby-on-rails-get-some-random-records/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Harry Ferguson Memorial CMS choice &#8211; Textpattern</title>
		<link>http://www.davidlowry.co.uk/268/harry-ferguson-memorial-cms-choice-textpattern/</link>
		<comments>http://www.davidlowry.co.uk/268/harry-ferguson-memorial-cms-choice-textpattern/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 15:24:54 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[harry ferguson memorial]]></category>
		<category><![CDATA[northern ireland.]]></category>
		<category><![CDATA[textpattern]]></category>
		<category><![CDATA[web design]]></category>
		<category><![CDATA[yab_blog]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=268</guid>
		<description><![CDATA[The last few days I&#8217;ve been putting the finishing touches to the CMS behind Harry Ferguson Memorial.com. Initially completely in hand-coded HTML (to get it looking right without distracting myself hacking around in a CMS template styling system), it was time to choose a CMS to allow a certain amount of user updating to be...<a href="http://www.davidlowry.co.uk/268/harry-ferguson-memorial-cms-choice-textpattern/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>The last few days I&#8217;ve been putting the finishing touches to the CMS behind <a href="http://harryfergusonmemorial.com">Harry Ferguson Memorial.com</a>. Initially completely in hand-coded HTML (to get it looking right without distracting myself hacking around in a CMS template styling system), it was time to choose a CMS to allow a certain amount of user updating to be done on the site.</p>
<p>I first experienced the <a href="http://www.textpattern.com/">Textpattern</a> CMS through my participation in the <del datetime="2009-09-02T13:58:28+00:00">TextDrive</del> <a href="http://www.joyent.com/">Joyent</a> Mixed Grill &#8220;Venture Capitalist&#8221; life-time subscription. I was initially baffled by it&#8217;s usage of &#8216;forms&#8217; &#8216;pages&#8217; and &#8216;sections&#8217; to categorise different design and structural hierarchy.  Mostly using <a href="http://www.wordpress.com/">WordPress</a> over the last few years <a href="http://www.dromorecathedral.co.uk/">on</a> <a href="http://www.davidlowry.co.uk/">various<a/> <a href="http://www.qubtennis.com/">sites</a> and most recently developing a custom Rails CMS for the upcoming <a href="http://www.bsni.co.uk/">Bible Society of Northern Ireland</a> website (note: current site is *not* of my creation) (launching in the next few weeks) I decided I needed to hunt around a bit. A quick play with Drupal left me unimpressed, I wasn&#8217;t going to touch Joomla (is it just me or does every site that uses it scream &#8220;look, it&#8217;s a site built using Joomla!&#8221;) and so I thought I&#8217;d take <a href="http://textpattern.com/">Textpattern</a> out for another try.</p>
<p>Impressed by the fact it&#8217;s finally had an update (<a href="http://textpattern.com/weblog/336/textpattern-cms-420-released">version 4.2.0<a/> was released just a week ago (28th August) &#8212; after, I believe, a long hiatus &#8212; time to give it a spin once more.</p>
<p><a href="http://textbook.textpattern.net/wiki/index.php?title=Detailed_Installation_Instructions">Installation is pretty simple</a>, quite lightweight; doesn&#8217;t require much in the way of configuration if you have a bog standard *AMP setup, just create or associate the database you wish to use (you can use a &#8216;_txp&#8217; style suffix to append onto your existing DB) and create the .htaccess requested &#8211; it&#8217;s all explained on the <a href="http://textbook.textpattern.net/wiki/index.php?title=Detailed_Installation_Instructions">linked instruction page.</a></p>
<p>I&#8217;m not going to do any sort of tutorial on the installation process, that&#8217;s been covered so many times, by so many people, I&#8217;d only do it a disservice. However, I have been using the <a href="http://www.yablo.de/article/356/textpattern-shop-yab_shop#tags">YAB_Shop plugin</a>, which is seriously lacking in documentation, with a <a href="http://forum.textpattern.com/viewtopic.php?id=26300">27 page forum post</a> over at <a href="http://www.textpattern.org/">Textpattern.org</a>. I&#8217;ll be putting together a few thoughts and instructions (which I found difficult to find) on the e-commerce plugin in the next few days and hopefully that&#8217;ll help someone out (and help me process and learn further!).</p>
<p>&#8212;-</p>
<p>Edit: I realise I didn&#8217;t really &#8216;review&#8217; the experience, which I intend to do more so when I write about the plugin, most of my gripes are with the plugin workflow!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/268/harry-ferguson-memorial-cms-choice-textpattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

