Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > can this be more succinct?

Reply
Thread Tools

can this be more succinct?

 
 
eggie5
Guest
Posts: n/a
 
      08-28-2007
How can this be more succinct? Can I somehow put the nil check inline
with the each?

unless(params[:categories].nil?)
params[:categories].each do |id|
@subscriber.subscriptions.create :category_id =>
id, :timestamp_start => Time.now.to_s, :is_subscribed => true
end
end

 
Reply With Quote
 
 
 
 
Daniel Lucraft
Guest
Posts: n/a
 
      08-28-2007
eggie5 wrote:
> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end



(params[:categories]||[]).each do |id|
@subscriber.subscriptions.create :category_id => id,
:timestamp_start => Time.now.to_s, :is_subscribed => true
end

best,
Dan
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Bertram Scharpf
Guest
Posts: n/a
 
      08-28-2007
Hi,

Am Dienstag, 28. Aug 2007, 23:00:08 +0900 schrieb eggie5:
> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end


Maybe:

pc = params[:categories]
pc and pc.each do |id|
...
end

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

 
Reply With Quote
 
Marcin Mielżyński
Guest
Posts: n/a
 
      08-28-2007
eggie5 wrote:
> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end
>


params[:categories].to_a.each do ...


lopex
 
Reply With Quote
 
Florian Aßmann
Guest
Posts: n/a
 
      08-28-2007
Hi eggie5,

> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end


Just a minor thingy, rename timestamp_start column to created_at and
Rails does its magic. So you can write (categories replaced with
category_ids!):

(params[:category_ids] || []).each do |category_id|
@subscriber.subscriptions.create(:category_id => category_id,
:is_subscribed => true)
end


You can also put a verify in the class definition of your controller:

class SubscriptionsController < ApplicationController
verify arams => :category_ids, :method => ost,
nly => :subscribe,
:redirect_to => :back

def subscribe
# ...
# both String and Array do have the each method...
params[:category_ids].each do |category_id|
@subscriber.subscriptions.create(:category_id => category_id,
:is_subscribed => true)
end
# ...
end

# ...
end


You can even move this whole bunch of code to the subscribers model itself:

class Subscriber < ActiveRecord::Base

# This methods accepts multiple category ids in form of:
# instance.subscribe 1, 2, 4, ...
def subscribe(*category_ids)
# wrap insertion of multiple records in a transactions is generally
# a good idea...
ActiveRecord::Base.transaction do
category_ids.each do |category_id|
subscriptions.create(:category_id => category_id,
:is_subscribed => true)
end
end
end

end

class SubscriptionsController < ApplicationController
verify arams => :category_ids, :method => ost,
nly => :subscribe,
:redirect_to => :back

def subscribe
# ...
@subscriber.subscribe(*params[:category_ids])
# ...
end

# ...
end


Or extend the association with a module, see AssociationExtensions in
ActiveRecord::Associations::ClassMethods. in this case you could write
something like:

instance.subscriptions.category_ids = category_ids

This encapsulate all methods in a module that are only important for the
association itself - it's clean!


If you don't use ActionPack and ActiveRecord at all don't mind this post...

Regards
Florian

P.S.
Beware, this code was not run through any functional or unit tests...

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-28-2007
On 28.08.2007 16:09, Bertram Scharpf wrote:
> Hi,
>
> Am Dienstag, 28. Aug 2007, 23:00:08 +0900 schrieb eggie5:
>> How can this be more succinct? Can I somehow put the nil check inline
>> with the each?
>>
>> unless(params[:categories].nil?)
>> params[:categories].each do |id|
>> @subscriber.subscriptions.create :category_id =>
>> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
>> end
>> end

>
> Maybe:
>
> pc = params[:categories]
> pc and pc.each do |id|
> ...
> end


+1

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
Tree view questions - Can I have more than one root? Can I de-link the current node? Alan Silver ASP .Net 3 11-09-2005 03:01 PM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Can a digital see more than we can? Gerald Ross Digital Photography 20 08-31-2004 03:35 PM
Re: With More Flash More Lumix: using an external flash unit with the FZ1 and other digicams Hans-Georg Michna Digital Photography 4 08-24-2003 06:05 PM



Advertisments