Maybe this gives you a better idea of what I am trying to do.  My current 
approach in the other methods is to pass in the dataset.  When I tried to 
set the dataset, I was corrupting the original dataset attribute on the 
Model.  That is why I was trying to clone it. 

def self.apply_filter(params)
    dataset = self.select_all

    # Convert the column symbols to strings
    columns = self.columns.map { |c| c.to_s }

    # Determine which attributes we have criteria for
    filter_criteria = columns & params.keys

    # Apply criteria to dataset
    filter_criteria.each do |attribute_name|
      attribute_value = "#{params[attribute_name].gsub('*','%')}%"
      dataset = dataset.where(Sequel.like(attribute_name.to_sym, 
attribute_value))
    end
    dataset
  end

  # order_by is a csv string of attribute names to order by.
  # A '-' at the beginning of the attribute name means to sort in desending 
order.
  # Otherwise, sort in ascending order, which is the default
  def self.apply_order_by(dataset, order_by)
    return dataset unless order_by.present?

    order_by = order_by.split(',')
    order_by.each do |attribute_name|
      attribute_name = attribute_name.strip
      if attribute_name.start_with? '-'
        symbol = attribute_name.gsub('-', '').to_sym
        dataset = dataset.order_append(Sequel.desc(symbol))
      else
        symbol = attribute_name.to_sym
        dataset = dataset.order_append(symbol)
      end
    end
    dataset
  end

  def self.apply_pagination(dataset, start_index, end_index, order_by=nil)
    dataset = dataset.limit(start_index.to_i..end_index.to_i)
    self.apply_order_by(dataset, order_by)
  end

In the controller:

    filtered_dataset = NetworkObject.apply_filter(params)
    filtered_dataset = filtered_dataset.where(:NODE_STATUS_ID => 1, 
:OBJECT_STATUS_ID => 1)
    @network_objects = NetworkObject.apply_pagination(filtered_dataset, 
params[:_startRow], params[:_endRow], "name").all

On Friday, August 9, 2013 10:19:58 AM UTC-5, Jeremy Evans wrote:
>
> On Friday, August 9, 2013 7:56:06 AM UTC-7, Keith Moore wrote:
>>
>> I am trying to do something similar to what the Sequel::Model does in 
>> terms of chaining dataset methods.  However, I am having some issues in 
>> doing this.  This may be more of a Ruby design question than a Sequel Gem 
>> question, 
>>
>> However, I am hoping someone can help me with this issue. Here is what I 
>> am trying to do: 
>>
>> Item < Sequel::Model(:ITEM)
>>
>> def self.apply_filter(params)
>> c = self.clone 
>> # loop
>> c.dataset = c.dataset.where(some criteria here)
>> # end loop
>> c
>> end
>>
>> end
>>
>>
>> And in my "controller", I want to be able to do something like the below 
>> where the other apply methods are chained together in a similar fashion.
>>
>> items = 
>> Item.apply_filter(params).apply_pagination(params).apply_order_by(params).all
>>
>> The problem is that I don't get back instances of type Item.  They are of 
>> type Class.
>>
>> Can someone tell me how I should go about achieving this?
>>
>
> Why do you want to clone the class?  If you want instances of type Item, 
> you shouldn't be cloning the Item class.  You probably just want to make 
> apply_filter a dataset method:
>
> class Item < Sequel::Model(:ITEM)
>   dataset_module do
>     def apply_filter(params)
>       where(some criteria here)
>     end
>   end
> end
>
> Note that this should work with the example code you provided.
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to