# Radiant 0.6.6 / Rails 2.0 Model Inheritance Question

**URL:** <https://www.ruby-forum.com/t/radiant-0-6-6-rails-2-0-model-inheritance-question/136111>\
**Category:** Radiant CMS\
**Created:** [April 29, 2008, 12:06am UTC](https://www.ruby-forum.com/t/radiant-0-6-6-rails-2-0-model-inheritance-question/136111 "2008-04-29T00:06:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 12:06am UTC](https://www.ruby-forum.com/t/radiant-0-6-6-rails-2-0-model-inheritance-question/136111/1 "2008-04-29T00:06:29Z")

</div>

Looks like I just sent out an email as HTML. Let’s try this again with  
plain text…

I’m reworking my Styles and Javascripts Extension to play nice with  
Rails 2.0, and incorporate some things that John suggested and I’m  
hitting a weird brick wall…

I have a TextAsset Model (with corresponding table) from which I inherit  
JavascriptAsset and StylesheetAsset Models (single table inheritance).  
I just moved one of my validation rules out of the parent (TextAsset)  
into StylesheetAsset and suddenly my tests are failing.

This should be easy but I’m stumped. (This is my first foray into rSpec  
but I’ve basically mimicked the way Radiant is testing it’s models).

Help anyone?

Here are the relevant pieces of offending code…

## From stylesheet\_asset.rb

class StylesheetAsset \< TextAsset  
validates\_uniqueness\_of :name, :message =\> ‘filename already in use’  
end

## From stylesheet\_asset\_spec.rb

require File.dirname( **FILE** ) + ‘/…/spec\_helper’

describe StylesheetAsset do  
scenario :stylesheets  
test\_helper :validations

before(:each) do  
@stylesheet\_asset = @model = StylesheetAsset.new(stylesheet\_params)  
end

…

it ‘should validate uniqueness of’ do  
assert\_invalid :name, ‘filename already in use’, ‘main.css’ \<–  
this fails!!!  
assert\_valid :name, ‘just-a-test’  
end

end

## From stylesheets\_scenario.rb

class StylesheetsScenario \< Scenario::Base

def load  
create\_stylesheet “main.css”, :raw\_content =\>  
‘body{background:white}’  
end

helpers do  
def create\_stylesheet(name, attributes={})  
create\_record :stylesheet\_asset, name.symbolize,  
stylesheet\_params(attributes.reverse\_merge(:name =\> name))  
end

```
def stylesheet_params(attributes={})
  name = attributes[:name] || unique_stylesheet_name
  {
    :name => name,
    :raw_content => "dummy content"
  }.merge(attributes)
end

private

  @@unique_stylesheet_name_call_count = 0
  def unique_stylesheet_name
    @@unique_stylesheet_name_call_count += 1
    "stylesheet-#{@@unique_stylesheet_name_call_count}"
  end

```

end

end

What I was hoping to do here was have the two child classes (Javascripts  
and Stylesheets) each enforce their own unique naming sets (since  
they’ll be effectively served from their own directories). This way,  
the form validation wouldn’t complain if you had both a “my\_file”  
stylesheet and a “my\_file” javascript since they would be served from  
/stylesheets/my\_file and /javascripts/my\_file respectively. (Two  
stylesheets or two javascripts with the same name would be a no-no,  
though).

Is this some misunderstanding of Single Table Inheritance on my part, a  
flaw with Radiant’s extension testing, or what?

Thanks for any assistance,  
-Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 1:27am UTC](https://www.ruby-forum.com/t/radiant-0-6-6-rails-2-0-model-inheritance-question/136111/2 "2008-04-29T01:27:20Z")

</div>

Chris,

Instead of moving it to the child class, I would put the validation in  
the TextAsset class, but put the scope on the validation to :type, i.e.

validates\_uniqueness of :name, :scope =\> :type, :message =\> ‘filename  
already in use’

Sean

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/radiant-0-6-6-rails-2-0-model-inheritance-question/136111/3 "2008-04-29T16:42:05Z")

</div>

Thanks Sean, I completely forgot about :scope.

This didn’t completely fix it for me, though. So, in case this helps  
anyone in the future, I still had to change the following…

1. I’m mirroring Radiant’s use of:  
set\_inheritance\_column :class\_name  
instead of using the default Rails ‘type’ column for single table  
inheritance. So of course my code reads:  
validates\_uniqueness of :name, :scope =\> :class\_name, :message  
=\> ‘filename already in use’

2. This still didn’t work, though. And it was only by poking around  
the Archive extension that I figured out that the create\_record  
call in the scenario had to explicitly call out the value for this  
class\_name field like (shown here in my create\_stylesheet helper):  
def create\_stylesheet(name, attributes={})  
create\_record :stylesheet\_asset,  
name.symbolize,

I’m still not sure why #2 was required (possible bug with Scenario  
plugin)? I would have thought that creating the record would have  
automatically used Rails STI and populated the class name for me.

It’s also possible that I wouldn’t have needed this if I’d have stuck to  
the default STI implementation and just used the ‘type’ field – didn’t  
try that one.

-Chris

---

<div class="post-metadata">

**Author:** ![mahadewa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@mahadewa](https://www.ruby-forum.com/u/mahadewa)\
**Post date:** [April 29, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/radiant-0-6-6-rails-2-0-model-inheritance-question/136111/4 "2008-04-29T16:53:19Z")

</div>

> ```
> :class_name => 'StylesheetAsset') )
> end
> 
> ```
> 
> I’m still not sure why #2 was required (possible bug with Scenario  
> plugin)? I would have thought that creating the record would have  
> automatically used Rails STI and populated the class name for me.

Ah, that makes sense. Next time you can probably use create\_model  
instead, which creates the actual AR object instead of just writing the  
record to the DB.

Sean
