# Use of external DB and related data

**URL:** <https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421>\
**Category:** Radiant CMS\
**Created:** [December 5, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421 "2006-12-05T18:01:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sistrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sistrall](https://www.ruby-forum.com/u/sistrall)\
**Post date:** [December 5, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421/1 "2006-12-05T18:01:59Z")

</div>

Hello everybody,

I’m wondering if Radiant could be a convenient choice on a project  
I’m starting to work on and I’d be curious to hear your opinions.

The site I’m starting to develop is an almost regular site, for the  
main part made of simple content pages. For this part I’m pretty sure  
Radiant could work very well.

But the site will also display part of the data that are actually  
managed from the intranet site (made on Rails), and therefore should  
read data from a different database. I should also create a small  
authenticated area on the site in which, behind a login form, the  
users can download file, eventually upload documents, compile little  
survey, maintain their profile updated and so on.

This is a possible plan: I thought to develop a series of custom tags  
and behaviours to show in the pages the data coming from the intranet  
database. Would you think it could be an easy path to follow for  
doing what I have to do?

Thank you in advance for every help,  
Silvano

---

<div class="post-metadata">

**Author:** ![sistrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sistrall](https://www.ruby-forum.com/u/sistrall)\
**Post date:** [December 5, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421/2 "2006-12-05T22:44:46Z")

</div>

Silvano S. wrote:

> This is a possible plan: I thought to develop a series of custom tags  
> and behaviours to show in the pages the data coming from the intranet  
> database. Would you think it could be an easy path to follow for  
> doing what I have to do?
> 
> Thank you in advance for every help,  
> Silvano

I’ve done a similar project, displaying live data from an external  
database in a site managed by Radiant. Here are some details. (Apologies  
in advance to all the Rails/Ruby gurus for any gaffes in this code.)

===  
Here is a behavior to run Ruby code in a Radiant web page:

class RubycodeBehavior \< Behavior::Base  
register “Rubycode”  
def cache\_page?  
false  
end  
define\_tags do  
tag “evalpart” do |tag|  
partname = tag.attr[‘part’] || ‘ruby-code’  
t = eval(render\_page\_part(partname))  
end  
end  
end

===  
Here is a sample page that uses the Rubycode behavior to display Ruby  
generated code. It has three parts (tabs), named “body”, “ruby-code”,  
and “ruby-code-2”. (The names of the latter two aren’t important, but  
‘ruby-code’ is the default name for the \<r:evalpart /\> tag.)

Contents of the “body” part:

### Ruby-code

### Ruby-code-2

Contents of the “ruby-code” part:

a = “

Hello world!

\n”

Contents of the “ruby-code-2” part:

b = “

Hello world 2!

\n”

The key is that the code in any ‘eval-ed’ page part _must_ finish up by  
putting all its ‘output’ into a single variable that gets returned as  
the final step of the code.

===  
Some Rails tips (probably obvious to most).

Use ‘require\_dependency’, not ‘require’ to pull shared code into a ruby  
code page part. Using ‘require’ will fail (in development mode only?)  
when reloading an already displayed page.

Your models need a different database connection from the one set up by  
Radiant, without disturbing the Radiant connection. There are various  
ways to do this, but here is the method I’m using. Set up an  
intermediate class that descends from ActiveRecord::Base, like so:

class Mybase \< ActiveRecord::Base  
self.abstract\_class = true # \<-- Important. Done so  
# ActiveRecord doesn’t think descendants of Mybase are  
# doing single table inheritance.  
end

Mybase.establish\_connection(  
# put new connection info here …  
)

Then make all your models descend from Mybase.

David Peoples

---

<div class="post-metadata">

**Author:** ![sistrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sistrall](https://www.ruby-forum.com/u/sistrall)\
**Post date:** [December 6, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421/3 "2006-12-06T12:53:18Z")

</div>

Hi David,

Il giorno 05/dic/06, alle ore 22:43, David Peoples ha scritto:

> I’ve done a similar project, displaying live data from an external  
> database in a site managed by Radiant. Here are some details.  
> (Apologies  
> in advance to all the Rails/Ruby gurus for any gaffes in this code.)

Knowing that it’s possible (and it seems easy) is a big thing. 🙂

> ===  
> Here is a behavior to run Ruby code in a Radiant web page:

I don’t think I’ll have to put ruby code in page parts, but I’m happy  
to know that I could.

> end
> 
> Mybase.establish\_connection(  
> # put new connection info here …  
> )
> 
> Then make all your models descend from Mybase.

That’s similar to what I had in mind.

Thank you for your useful information,  
Silvano

---

<div class="post-metadata">

**Author:** ![sistrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sistrall](https://www.ruby-forum.com/u/sistrall)\
**Post date:** [December 6, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421/4 "2006-12-06T16:29:46Z")

</div>

Silvano S. wrote:

> Hi David,
> 
> Il giorno 05/dic/06, alle ore 22:43, David Peoples ha scritto:
> 
> > ===  
> > Here is a behavior to run Ruby code in a Radiant web page:
> 
> I don’t think I’ll have to put ruby code in page parts, but I’m happy  
> to know that I could.

Yes, its possible to store the code elsewhere, such as in text files on  
disk. The behavior I wrote has a tag to do that as well. I just decided  
storing and editing all of it in one place (the CMS database) was the  
simplest solution.

You mentioned in your original note the need to require authentication  
for a section of your planned site. That is a feature I want too, but I  
don’t know how to do it yet. If you find a solution, please post it back  
to this mailing list so I can see how you did it.

David

–  
David Peoples removed\_email\_address@domain.invalid  
The Touring Cyclist [http://www.touringcyclist.com](http://www.touringcyclist.com)  
11816 St. Charles Rock Road, Bridgeton, MO 63044  
tel: 314-739-4648 fax: 314-739-4972

---

<div class="post-metadata">

**Author:** ![sistrall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@sistrall](https://www.ruby-forum.com/u/sistrall)\
**Post date:** [December 6, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/use-of-external-db-and-related-data/78421/5 "2006-12-06T16:47:48Z")

</div>

Il giorno 06/dic/06, alle ore 16:28, David Peoples ha scritto:

> Yes, its possible to store the code elsewhere, such as in text  
> files on  
> disk. The behavior I wrote has a tag to do that as well. I just  
> decided  
> storing and editing all of it in one place (the CMS database) was the  
> simplest solution.

It’s just a question of taste: I don’t like Radiant users can view a  
mix of Radiant tags and Ruby code when composing pages. Anyway, I  
think your solution can be very handy in many cases.

> You mentioned in your original note the need to require authentication  
> for a section of your planned site. That is a feature I want too,  
> but I  
> don’t know how to do it yet. If you find a solution, please post it  
> back  
> to this mailing list so I can see how you did it.

It could take me a while: I’ve just started to work on the graphic  
design of the site. It will probably go online sometimes between  
january and february: I would post my solution here when I’ll have  
something working.

> David

Silvano
