DRY a named scope

Is there a way to DRY this up a bit?

named_scope :has_valid_sysoid, lambda{|sysoid|
(sysoid.nil?) ? {:conditions => [“nodesysoid IS NOT NULL AND
nodelabel LIKE ‘%-to-%’”], :include => [:ipinterface, :alarm]} :
{:conditions => ["nodesysoid IS NOT NULL AND nodelabel LIKE ‘%-to-%’
AND nodesysoid = ? ", sysoid], :include => [:ipinterface, :alarm]}
}

Thanks.

Trying to keep the controller simple:
Takes a value or not.
One thing I could do was put he include inside the method call but
where does the param go?

Node.has_valid_sysoid(…)

The scope is working, just trying cut out the duplication.

On Jun 24, 2010, at 3:34 PM, Me wrote:

Is there a way to DRY this up a bit?

named_scope :has_valid_sysoid, lambda{|sysoid|
(sysoid.nil?) ? {:conditions => [“nodesysoid IS NOT NULL AND
nodelabel LIKE ‘%-to-%’”], :include => [:ipinterface, :alarm]} :
{:conditions => ["nodesysoid IS NOT NULL AND nodelabel LIKE ‘%-to-%’
AND nodesysoid = ? ", sysoid], :include => [:ipinterface, :alarm]}
}

You could move the condition inside the {}'s so it only affects the
value of the :conditions key.

But… I would be more included to separate the scopes into
:has_valid_sysoid and :has_specific_sysoid.

Model.has_valid_sysoid(‘foo’) doesn’t really seem to be doing what the
scope suggests it’s doing… to me anyway… I see a difference b/n
“valid” and “a specific”.

I don’t know if that makes sense for your application or not…

-philip

named_scope :has_valid_sysoid, lambda{|sysoid|
conditions = [“nodesysoid IS NOT NULL AND nodelabel LIKE ‘%-to-%’]
unless sysoid.nil?
conditions[0]<< " AND nodesysoid = ?”
conditions << sysoid
end
{:conditions => conditions, :include => [:ipinterface, :alarm]}
}

> >> > } >> > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >


Herr Bente P.,
Weidenallee 37
20357, Hamburg

Tel.: 0152-22361035

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs