librelist archives

« back to archive

Not sure I understand categories

Not sure I understand categories

From:
Stevan C Wing
Date:
2012-12-05 @ 18:04
Hi everyone.

I have googled around for a while looked through the methods in the gem 
and search the liberalist archive.

I wanted to know if there was a way I can filter by an articles category 
and then have a page template display all of those articles with the 
category? I am sure that this was the purpose of categories but I cannot 
find out how to achieve it.

e.g. 

Categories: jokes, insulting

This article would be displayed on articles list for:
/jokes and /insulting and /blog

I am not sure how to 'filter' the Categories metadata in Ruby and I cannot
even get this to work:

      def my_pages
        Nesta::Page.find_all.select { |article| article.flagged_as?('wow') }
      end

with 'Flag: wow' in the article metadata.


Any ideas?

Re: [nesta] Not sure I understand categories

From:
James Abbott
Date:
2012-12-07 @ 12:48
>
> I am not sure how to 'filter' the Categories metadata in Ruby and I cannot
> even get this to work:
>
>       def my_pages
>         Nesta::Page.find_all.select { |article| article.flagged_as?('wow')
> }
>       end
>
> with 'Flag: wow' in the article metadata.
>

Your "my_pages" method looks correct (disclaimer: I haven't run it), but
you've got a typing error in your metadata declaration. It's "Flags: wow",
not "Flag".

Refer to the method body (highlighted):
https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L126

Regarding parsing out an article's categories, maybe you can bootstrap
something off of some of the methods that Nesta gives you (below)?

https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L276
https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L324

Cheers,
James


On Wed, Dec 5, 2012 at 7:04 PM, Stevan C Wing <stevancw@me.com> wrote:

> I am not sure how to 'filter' the Categories metadata in Ruby and I cannot
> even get this to work:
>
>       def my_pages
>         Nesta::Page.find_all.select { |article| article.flagged_as?('wow')
> }
>       end
>
> with 'Flag: wow' in the article metadata.
>

Re: [nesta] Not sure I understand categories

From:
Stevan C Wing
Date:
2012-12-08 @ 23:16
Thanks James,

You're right the typo seemed to fix the issues.
I'll take a look into building a similar method for returning categories 
as the flagged_as? method?

Cheers,

Stevan

On 7 Dec 2012, at 12:48, James Abbott <abbottjam@gmail.com> wrote:

> I am not sure how to 'filter' the Categories metadata in Ruby and I 
cannot even get this to work:
> 
>       def my_pages
>         Nesta::Page.find_all.select { |article| article.flagged_as?('wow') }
>       end
> 
> with 'Flag: wow' in the article metadata.
> 
> Your "my_pages" method looks correct (disclaimer: I haven't run it), but
you've got a typing error in your metadata declaration. It's "Flags: wow",
not "Flag".
> 
> Refer to the method body (highlighted):
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L126
> 
> Regarding parsing out an article's categories, maybe you can bootstrap 
something off of some of the methods that Nesta gives you (below)?
> 
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L276
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L324
> 
> Cheers,
> James   
> 
> 
> On Wed, Dec 5, 2012 at 7:04 PM, Stevan C Wing <stevancw@me.com> wrote:
> I am not sure how to 'filter' the Categories metadata in Ruby and I 
cannot even get this to work:
> 
>       def my_pages
>         Nesta::Page.find_all.select { |article| article.flagged_as?('wow') }
>       end
> 
> with 'Flag: wow' in the article metadata.
> 

Re: [nesta] Not sure I understand categories

From:
James Abbott
Date:
2012-12-09 @ 12:07
A method for returning categories already exists: it's the "categories"
method on the Page class:

https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L276

It returns an array of page objects (line 278), one page object for each
category that "this" page has. It's a mapping from "this" page's category
strings to pages represented by those strings (lines 277-278).

Are you sure that what you're trying to do cannot piggyback on this method?
(I mean, why re-invent the wheel?)

/ James


On Sun, Dec 9, 2012 at 12:16 AM, Stevan C Wing <stevancw@me.com> wrote:

> Thanks James,
>
> You're right the typo seemed to fix the issues.
> I'll take a look into building a similar method for returning categories
> as the flagged_as? method?
>
> Cheers,
>
> Stevan
> **
> On 7 Dec 2012, at 12:48, James Abbott <abbottjam@gmail.com> wrote:
>
> I am not sure how to 'filter' the Categories metadata in Ruby and I cannot
>> even get this to work:
>>
>>       def my_pages
>>         Nesta::Page.find_all.select { |article|
>> article.flagged_as?('wow') }
>>       end
>>
>> with 'Flag: wow' in the article metadata.
>>
>
> Your "my_pages" method looks correct (disclaimer: I haven't run it), but
> you've got a typing error in your metadata declaration. It's "Flags: wow",
> not "Flag".
>
> Refer to the method body (highlighted):
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L126
>
> Regarding parsing out an article's categories, maybe you can bootstrap
> something off of some of the methods that Nesta gives you (below)?
>
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L276
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L324
>
> Cheers,
> James
>
>
> On Wed, Dec 5, 2012 at 7:04 PM, Stevan C Wing <stevancw@me.com> wrote:
>
>> I am not sure how to 'filter' the Categories metadata in Ruby and I
>> cannot even get this to work:
>>
>>       def my_pages
>>         Nesta::Page.find_all.select { |article|
>> article.flagged_as?('wow') }
>>       end
>>
>> with 'Flag: wow' in the article metadata.
>
>
>
>

Re: [nesta] Not sure I understand categories

From:
Graham Ashton
Date:
2012-12-10 @ 18:30
On 9 Dec 2012, at 12:07, James Abbott <abbottjam@gmail.com> wrote:

> It returns an array of page objects (line 278), one page object for each
category that "this" page has. It's a mapping from "this" page's category 
strings to pages represented by those strings (lines 277-278).
> 
> Are you sure that what you're trying to do cannot piggyback on this 
method? (I mean, why re-invent the wheel?)

You can go the other way too, listing all the pages that are in a category:

  Nesta::Page.find_by_path('path/to/category').pages

If you just want the blog post, do this instead:

  Nesta::Page.find_by_path('path/to/category').articles

Stevan - reading your original question (quoted)...

>> I wanted to know if there was a way I can filter by an articles 
category and then have a page template display all of those articles with 
the category? I am sure that this was the purpose of categories but I 
cannot find out how to achieve it.

...I'm not sure what you're after. If you'd like to display all the 
articles in a category, on a page of their own, wouldn't that be the 
category page?

Maybe I've misread your question there, but if this is what you're after, 
try this.

If you put 3 articles in a category called 'foo/bar' (by setting the 
Categories metadata to foo/bar), create a page called 
content/pages/foo/bar.mdown. It'll list the relevant pages if your 
template contains the right code to iterate over them. The default 
templates do it for you (have a look in the views directory in the GitHub 
repository [1]), so you could steal the code from there (but it's 
basically the two method calls I showed you above).

[1] https://github.com/gma/nesta/blob/master/views/page.haml

It might look like that page is doing something completely different, but 
lines 9 and 15 (with their calls to the `pages` and `articles` methods) 
are where it happens. @page just happens to be set to the object that's 
returned by find_by_path.

--
Graham Ashton
Founder, The Agile Planner
http://www.theagileplanner.com | @agileplanner | @grahamashton