I decided to spend a little longer coming up with a better solution,
so went back to the original rails ordinalize method and instead now
regex the string in place.
I include it here just in case its useful to someone. My knowledge of
regex is low so I make no claims to its robustness.
start_date = page.start_date
sdate = start_date.strftime("%A") + " " +
start_date.strftime(“%d”).to_i.ordinalize + " " +
start_date.strftime(“%B”)
%{ #{sdate.gsub(/(.)(\d+)(st|nd|rd|th)(.)/) {|s| $1 + $2 +
‘’ + $3 + ‘’ + $4} } }
Thanks for all your help, and special thanks to Sean for the shards
and schedular extentions which helped me to dive into the code (for
better or worse!)
[Radiant] How to include classes in extensions?
subsorama at gmail.com subsorama at gmail.com
Fri Dec 7 19:22:24 GMT 2007
Previous message: [Radiant] How to include classes in extensions?
Next message: [Radiant] Issues while Installing backdoor
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Many thanks for both your detailed answers, they were indeed very
helpful. I went ahead with the mixin module as I also wanted the
ordinals styled with 's. After some thought I should imagine a
much cleaner way would be by defining a regexp for the date string and
running that from the tags, but as you can probably tell I’m learning
as I go. I’m not sure well formed the tags are on the other side but
it’s seems to be working as I wanted.
For your pleasure:
start_date = page.start_date
sdate = start_date.strftime("%A") + " " +
start_date.strftime(“%d”).to_i.ordinalize + " " +
start_date.strftime(“%B”)
sdate
Its probably not the place to ask, but if anyone knows a simple way to
replace those ordinals in the string using regexp I would be very
gratefiul!
Thanks once again for your all help,
Dominic
On 7 Dec 2007, at 14:18, Andrew O’Brien wrote:
Hi Dominic,
First off, I think what you’re trying to do is already done by a
Rails
core extension to Integer. See:
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Integer/Inflections.html
If you still want to do something like this, it needs to be more like
(and I haven’t tested this code):module Ordinalizable
def ordinalize
cardinal = self.to_i.abs
if (10…20).include?(cardinal) then
cardinal.to_s << ‘th’
else
cardinal.to_s << %w{th st nd rd th th th th th th}[cardinal %
10]
end
end
endOnly Integers should be ordinalized
Integer.send :include, Ordinalizable
Stick this in your extension’s lib directory and explicitly require
it
from the extension file.Your first example where you monkey-patch Numeric should have worked
(depending of course on if the file was loaded), however, like I said
above, Floats and most of the other Numerics probably shouldn’t be
ordinalized. I think module mixin is generally cleaner from a design
standpoint (opinions may vary), though.Your second one has a couple of problems, which I point out not out
of
toolishness, but in the hope that it’s helpful:
Numeric doesn’t need to inherit from Page. This is only necessary
for new Page types, which will appear in the Page Type select box.Putting a class inside an extension is often a good way to keep
your namespace clean, but you probably shouldn’t do it for anything
that inherits from ActiveRecord::Base.It looks like what you’ve got is a case of mixed metaphors here:
you’re trying to mixin a module to put an instance method into a
class, and you’re trying to monkey-patch the class directly. Both
are
fine on their own (with varying opinions on cleanliness), but neither
will work this way.You don’t want to include the module into Page. Since ordinalize
assumes that self is an Integer, it doesn’t make any sense for it to
operate on a Page. If, however, you defined a module with a number
of
tags (which it sounds like you’re also trying to do elsewhere), that
is what you want to do to get the tags into Page.Hope this helps,
AndrewOn Dec 7, 2007 7:32 AM, wrote:
Hi,
I would like to access this snippet of code from within my extension
tags:class Numeric
def ordinal
cardinal = self.to_i.abs
if (10…20).include?(cardinal) then
cardinal.to_s << ‘th’
else
cardinal.to_s << %w{th st nd rd th th th th th th}[cardinal %
10]
end
end
endAs the tags get called on the page I have tried adding the above to:
module StartDate::PageExtensions
class Numeric < Pagedef ordinal cardinal = self.to_i.abs if (10...20).include?(cardinal) then cardinal.to_s << 'th' else cardinal.to_s << %w{th st nd rd th th th th th th}
[cardinal %
10]
end
endend
endwhich is called from:
Page.send :include, StartDate::PageExtensions
The tag works fine when I don’t call the new method, but when I do
I’m
getting an “undefined method ‘ordinal’ error”, so I must be defining
it wrong, but how do I do it right?Any advice?
Thanks,
Dominic
Radiant mailing list
Post: Radiant at lists.radiantcms.org
Search: http://radiantcms.org/mailing-list/search/
Site: http://lists.radiantcms.org/mailman/listinfo/radiant
Radiant mailing list
Post: Radiant at lists.radiantcms.org
Search: http://radiantcms.org/mailing-list/search/
Site: http://lists.radiantcms.org/mailman/listinfo/radiant
Previous message: [Radiant] How to include classes in extensions?
Next message: [Radiant] Issues while Installing backdoor
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Radiant mailing list