librelist archives

« back to archive

Monkey-patching the Page class

Monkey-patching the Page class

From:
James Abbott
Date:
2011-08-16 @ 13:40
Hi,-

When tidying up the code that injects "category" classes into the HTML body
tag, I created a "models.rb" at the root level and put this method into it:

module Nesta
>   class Page < FileModel
>     def is_category?
>       !pages.empty? or !articles.empty?
>     end
>   end
> end
>

so that in app.rb, I can call it with:

> @body_class = 'category ' + @body_class if @page.is_category?
>

instead of:

> @body_class = 'category ' + @body_class unless @page.pages.empty? and
> @page.articles.empty?
>

The app won't recognize the new method though -  how do I get it to work? It
seems like an appropriate method for a page object to have.

/ James

Re: [nesta] Monkey-patching the Page class

From:
Graham Ashton
Date:
2011-08-16 @ 15:06
On 16 Aug 2011, at 14:40, James Abbott wrote:

> module Nesta
>   class Page < FileModel
>     def is_category?
>       !pages.empty? or !articles.empty?
>     end
>   end
> end
> 
> The app won't recognize the new method though -  how do I get it to work?

You don't need "< FileModel" as the class has already been defined; all 
you're doing is adding new methods.

See if that fixes it...

Re: [nesta] Monkey-patching the Page class

From:
James Abbott
Date:
2011-08-16 @ 15:10
>
> See if that fixes it...
>

It did.

/ James

On Tue, Aug 16, 2011 at 5:06 PM, Graham Ashton <graham@effectif.com> wrote:

> On 16 Aug 2011, at 14:40, James Abbott wrote:
>
> > module Nesta
> >   class Page < FileModel
> >     def is_category?
> >       !pages.empty? or !articles.empty?
> >     end
> >   end
> > end
> >
> > The app won't recognize the new method though -  how do I get it to work?
>
> You don't need "< FileModel" as the class has already been defined; all
> you're doing is adding new methods.
>
> See if that fixes it...
>

Re: [nesta] Monkey-patching the Page class

From:
Abel Rios
Date:
2011-08-16 @ 14:37
Hi James,

I created this gist a while ago, but shows the basic idea of how to lay out
your app.rb file: https://gist.github.com/918143. It seems that you're
trying to separate code in a module. I have yet to figure out how to get
that to work like I want, but I will when I have more time.

Hope this helps in the mean time.

- Abel

On Tue, Aug 16, 2011 at 8:40 AM, James Abbott <abbottjam@gmail.com> wrote:

> Hi,-
>
> When tidying up the code that injects "category" classes into the HTML body
> tag, I created a "models.rb" at the root level and put this method into it:
>
> module Nesta
>>   class Page < FileModel
>>     def is_category?
>>       !pages.empty? or !articles.empty?
>>     end
>>   end
>> end
>>
>
> so that in app.rb, I can call it with:
>
>> @body_class = 'category ' + @body_class if @page.is_category?
>>
>
> instead of:
>
>> @body_class = 'category ' + @body_class unless @page.pages.empty? and
>> @page.articles.empty?
>>
>
> The app won't recognize the new method though -  how do I get it to work?
> It seems like an appropriate method for a page object to have.
>
> / James
>

Re: [nesta] Monkey-patching the Page class

From:
James Abbott
Date:
2011-08-16 @ 15:03
Hi Abel,-

thanks much, it did help! It seems like the entry point for all
monkey-patching in Sinatra is the app.rb file, so even if one wants to
augment a file that's external to app.rb one has to go through the latter to
do so.

Cheers!

/ James

On Tue, Aug 16, 2011 at 4:37 PM, Abel Rios <abel@airios.com> wrote:

> Hi James,
>
> I created this gist a while ago, but shows the basic idea of how to lay out
> your app.rb file: https://gist.github.com/918143. It seems that you're
> trying to separate code in a module. I have yet to figure out how to get
> that to work like I want, but I will when I have more time.
>
> Hope this helps in the mean time.
>
> - Abel
>
>
> On Tue, Aug 16, 2011 at 8:40 AM, James Abbott <abbottjam@gmail.com> wrote:
>
>> Hi,-
>>
>> When tidying up the code that injects "category" classes into the HTML
>> body tag, I created a "models.rb" at the root level and put this method into
>> it:
>>
>> module Nesta
>>>   class Page < FileModel
>>>     def is_category?
>>>       !pages.empty? or !articles.empty?
>>>     end
>>>   end
>>> end
>>>
>>
>> so that in app.rb, I can call it with:
>>
>>> @body_class = 'category ' + @body_class if @page.is_category?
>>>
>>
>> instead of:
>>
>>> @body_class = 'category ' + @body_class unless @page.pages.empty? and
>>> @page.articles.empty?
>>>
>>
>> The app won't recognize the new method though -  how do I get it to work?
>> It seems like an appropriate method for a page object to have.
>>
>> / James
>>
>
>