26 Jul '13, 2pm

Railsでフォームオブジェクトを使った検索を簡単に実装する方法 | TechRacho #ruby #rubyjp

# 検索フォームのための汎用モデル class SearchForm include ActiveAttr::Model class_attribute :_search_model class_attribute :_like_attributes class_attribute :_equal_attributes class_attribute :_join_tables self._like_attributes = [] self._equal_attributes = [] self._join_tables = [] class << self private def inherited(child) child._search_model = child.name.gsub('SearchForm', '').constantize end def define_attribute(*attrs) attrs.each do |attr| if attr.respond_to?(:each) attr.each do |attr2| __send__(:attribute, attr2) unless attributes.include?(attr2) end else __send__(:attribute, attr) unless attributes.include?(attr) end end end def search_model(attr) self._search_model = attr end def like_attributes(*attrs) define_attribute attrs if attrs.respond_to?(:each) attrs.each do |attr| self._like_attributes += [attr] end else self._like_attributes += [attrs[0]] end end def equal_attributes(*...

Full article: http://techracho.bpsinc.jp/morimorihoge/2013_07_26/12552

Tweets