Use nanoc and ActiveRecord together

126 views
Skip to first unread message

Yen-Ju Chen

unread,
Jan 31, 2012, 4:10:26 AM1/31/12
to nanoc
I guess this might interest someone. I use Rails for data management
since the CRUD approach is easy. But hosting Rails app takes some
effort and money. Google App Engine can host static pages for free and
nanoc can generate static pages. So I figure out how to access data in
Rails from nanoc. I put the following codes in lib/config.rb in a
nanoc project so that it is loaded automatically. After that, model of
Rails can be access in nanoc automatically.

require 'active_record'
require 'sqlite3'

RAILS_ROOT ||= "/path_to_your_rails_app"
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
:database => "#{RAILS_ROOT}/db/
production.sqlite3")

Dir.glob("#{RAILS_ROOT}/app/models/*.rb").each do |r| require r end
Dir.glob("#{RAILS_ROOT}/app/models/*/*.rb").each do |r| require r end

(Originally posted in http://blog.berylliumwork.com/2012/01/use-activerecord-without-rails.html)

Denis Defreyne

unread,
Feb 4, 2012, 1:45:26 AM2/4/12
to na...@googlegroups.com
Hi,

It is indeed possible use ActiveRecord (or other ORMs or database libraries) in combination with nanoc, but there is one thing you need to be aware of. nanoc perform dependency tracking between Nanoc3::Items generated by data sources, and it cannot know whether arbitrary data will have changed and necessitates a recompilation of the site. nanoc also assumes all helper functions are pure, meaning that the return value is in function of the input parameters (actual parameters as well as @item, @layout etc).

For example, if you were to put the results of a raw SQL query somewhere on an item or a layout (e.g. <%= sql('SELECT title FROM posts').join %>) and you compile once, then change the “posts” table and recompile, nanoc will not recompile that page a second time, because it has no way of knowing that the posts table has changed. Similarly, don’t include file contents read directly off the disk (e.g. <%= YAML.load_file('translations.yml')['my_trans_key']['en'] %>.

The recommended approach to using external data is to write a data source for them and wrap them in Nanoc3::Items.

Cheers,

Denis

> --
> You received this message because you are subscribed to the nanoc discusssion group.
>
> To post to this group, send email to na...@googlegroups.com
> To unsubscribe from this group, send email to
> nanoc+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nanoc?hl=en

Jim Mendenhall

unread,
Feb 26, 2012, 1:45:31 PM2/26/12
to na...@googlegroups.com
Denis,
That sounds great. Could you point us to an example of how to make a custom data source with ActiveRecord and wrap it in Nanoc3::Items? 
Jim

Denis Defreyne

unread,
Mar 4, 2012, 2:00:30 PM3/4/12
to na...@googlegroups.com
Hi Jim,

I created an example of how ActiveRecord (or other ORMs) could be used together with nanoc. You can find it here:

https://github.com/ddfreyne/nanoc-activerecord-example

Cheers,

Denis

> > nanoc+un...@googlegroups.com


> > For more options, visit this group at
> > http://groups.google.com/group/nanoc?hl=en
>
>
>
>
>
>
>
>
>
>

> --
> You received this message because you are subscribed to the nanoc discusssion group.
>
> To post to this group, send email to na...@googlegroups.com
> To unsubscribe from this group, send email to

> nanoc+un...@googlegroups.com

signature.asc

Jim Mendenhall

unread,
Mar 4, 2012, 2:48:33 PM3/4/12
to nanoc
Wow, thank you Denis! This will help me a lot and hopefully some
others too.

On Mar 4, 11:00 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:
> > > (Originally posted inhttp://blog.berylliumwork.com/2012/01/use-activerecord-without-rails....)
>
> > > --
> > > You received this message because you are subscribed to the nanoc discusssion group.
>
> > > To post to this group, send email to na...@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > nanoc+un...@googlegroups.com
> > > For more options, visit this group at
> > >http://groups.google.com/group/nanoc?hl=en
>
> > --
> > You received this message because you are subscribed to the nanoc discusssion group.
>
> > To post to this group, send email to na...@googlegroups.com
> > To unsubscribe from this group, send email to
> > nanoc+un...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/nanoc?hl=en
>
>
>
>  signature.asc
> < 1KViewDownload

Christian Nunciato

unread,
Oct 4, 2013, 3:26:08 PM10/4/13
to na...@googlegroups.com
Just FYI, this link is 404ing.

Denis Defreyne

unread,
Oct 4, 2013, 3:29:16 PM10/4/13
to na...@googlegroups.com
Oops—thanks for letting me know! The repository now lives under the “nanoc” organisation on GitHub:


Cheers

Denis

GPG key fingerprint:
02D7 06C2 7383 C9AE 5A6B 8942 A6EB 2130 80E7 63A7

--
--
You received this message because you are subscribed to the nanoc discusssion group.
 
To post to this group, send email to na...@googlegroups.com
To unsubscribe from this group, send email to
nanoc+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nanoc?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nanoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanoc+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

signature.asc
Reply all
Reply to author
Forward
0 new messages