ruby 一个简单的时代sósimplificare ganhar todososmétodossemprecisar fazer muito,émuitosimples

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 一个简单的时代sósimplificare ganhar todososmétodossemprecisar fazer muito,émuitosimples相关的知识,希望对你有一定的参考价值。

module Searchable
  included do
    scope :gender, ->(gender) { where(:gender => gender) }
    scope :birth_on, ->(date) { where(:birthdate =>date) }
    scope :birth_before_than, ->(date) { where("#{quoted_table_name}.birthdate < ?", date) }
    scope :birth_after_than, ->(date) { where("#{quoted_table_name}.birthdate > ?", date) }
    scope :profile, ->(profile){ where(:profile => profile) }
  end

  module ClassMethods
    def fiter_with(options = {})
       relation = all
       options.each do |key, value| 
         relation = relation.send(key, value) if available_scope?(key)
       end
       relation
    end

    def available_scope?(scope)
      %w(gender birth_on birth_before_than birth_after_than profile).include?(scope)
    end
  end
end


# Exemplo:
# search = MySearch.new(User)
# search.filter_with(options)
class MySearch
  attr_accessor :klass

  def initialize klass
    @klass = klass
    add_scopes(@klass)
  end

  def add_scopes(klass)
    @klass.class_eval do
      scope :gender, ->(gender) { where(:gender => gender) }
      scope :birth_on, ->(date) { where(:birthdate =>date) }
      scope :birth_before_than, ->(date) { where("#{quoted_table_name}.birthdate < ?", date) }
      scope :birth_after_than, ->(date) { where("#{quoted_table_name}.birthdate > ?", date) }
      scope :profile, ->(profile){ where(:profile => profile) }
    end
  end

  def fiter_with(options = {})
    relation = @klass.all
      options.each do |key, value| 
        relation = relation.send(key, value) if available_scope?(key)
      end
    relation
  end

  private 

  def available_scope?(scope)
    %w(gender birth_on birth_before_than birth_after_than profile).include?(scope)
  end

end

以上是关于ruby 一个简单的时代sósimplificare ganhar todososmétodossemprecisar fazer muito,émuitosimples的主要内容,如果未能解决你的问题,请参考以下文章

ruby Códigodelnivel 9,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 7,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 6,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 5,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 4,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 9,modo Intermediate