際際滷

際際滷Share a Scribd company logo
Ransack, Ransack, Ransack
Agileware kaigi 1
2014.7.25 @maimai77
Searching ActiveRecord!
匯崑碧はそこそこできる
Issue.where(name: 'hoge').to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE `issues`.`name` = 'hoge'"
Issue.where(name: ['hoge', 'fuga']).to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE `issues`.`name` IN ('hoge', 'fuga')"
Searching ActiveRecord...
寄弌曳^、何蛍匯崑は誼吭じゃない
Issue.where("id < ?", 3).to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE (id < 3)"
Issue.where("name LIKE ?", '%hoge%').to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE (name LIKE '%hoge%')"
仭くなってきた´
Searching ActiveRecord ;<
OR碧になるともうダメな徨
4 Arel ★ 辛i來 #とは
4 鏡徭の碧クラス ★ lがメンテすんの
4 伏SQL ★ 伉が孵れそうだ´
Ransack
What is Ransack?
ransack [r??ns?k]
ヾ侭をゝくまなく冥る[冥す], あさり指る
What is Ransack?
ransack gem
https://github.com/activerecord-hackery/ransack
4 ActiveRecordをして碧C嬬をつけるgem
4 O協なしで匯宥りの碧をカバ`
4 high googleability ★ googleトップがgithub
4 ドキュメントが樋
How to use Ransack
In your Gemfile:
gem 'ransack'
> bundle install
Rails4, 4.1喘に恷m晒されたbranchもある
Extend ActiveRecord
>bundle exec rails g model Issue name:string
>bundle exec rake db:migrate
class Issue < ActiveRecord::Base
end
で
Issue.search(name_cont: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE (`issues`.`name` LIKE '%hoge%')"
Search argument keys
Issue.search(name_cont: 'hoge').result
ransackable_attribute + predicate(峰Z)
ex)
4 name_eq
4 name_cont
Ransackable_attributes
Issue.ransackable_attributes
#=> ["id", "name", "created_at", "updated_at"]
defaultはIssue.column_namesと揖じ
Predicates
eq, gt, gteq, lt, lteq, in, cont, start, end and more
https://github.com/activerecord-hackery/ransack/
wiki/Basic-Searching
Alias method
class Issue < ActiveRecord::Base
def self.search
'hoge'
end
end
Issue.search
#=> 'hoge'
Issue.ransack(name_cont: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE (`issues`.`name` LIKE '%hoge%')"
このあたりまではパ`フ
ェクトRuby on Railsにも
ちらっといてます。
More Ransack
Complex search
4 2つのカラムのいずれかが峺協した、頒志
4 v銭枠のテ`ブルを碧する
Complex search
4 2つのカラムのいずれかが峺協した、頒志
Issue.ransack(name_or_memo_eq: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE ((`issues`.`name` = 'hoge' OR `issues`.`memo` = 'hoge'))"
and, or でY栽した ransackable_attribute も
ransackable
Complex search
4 v銭枠のテ`ブルを碧する
class Issue < ActiveRecord::Base
belong_to :project
end
Issue.search(project_name_eq: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# LEFT OUTER JOIN `projects` ON `projects`.`id` = `issues`.`project_id`
# WHERE `projects`.`name` = 'hoge'"
association + ransackable_attribute も ransackable
More and more Ransack
More complex search
4 剃の猟忖双と匯崑するものを碧
4 2つのカラムの、鮟Y栽したものと曳^
4 磯叔スペ`スをoした碧
Mみ栽わせだけじゃなんとも´
★ ransacker
What is ransacker?
customized ransackable_attribute
ransackerメソッドで弖紗する
Define ransacker
4 剃の猟忖双と匯崑するものを碧
class Issue < ActiveRecord::Base
ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent|
parent.table[:name]
end
end
Issue.search(reversed_name_eq: 'hoge').result.to_sql
#=> "SELECT `issues`.* FROM `issues` WHERE `issues`.`name` = 'egoh'"
Define ransacker
formatter: 碧に局したvalueを紗垢する
proc { |v| v.reverse }.call('hoge')
#=> "egoh"
block: 碧鵑離ラムをQ協する
Define ransacker
4 2つのカラムの、鮟Y栽したものと曳^
class Address < ActiveRecord::Base
ransacker :address do |parent|
Arel::Nodes::NamedFunction.new(
'CONCAT', [parent.table[:region],parent.table[:city]]
)
end
end
Address.search(address_cont: '寄昜軒寄昜偏')
#=> "SELECT `addresses`.* FROM `addresses`
# WHERE (CONCAT(`addresses`.`region`, '', `addresses`.`city`)
# LIKE '%寄昜軒寄昜偏%')"
Define ransacker
4 磯叔スペ`スをoした碧
ransacker :name_without_spaces, formatter: proc { |v| v.gsub(' ', '') } do |parent|
Arel::Nodes::NamedFunction.new(
'REPLACE', [parent.table[:name], ' ', '']
)
end
Issue.search(name_without_spaces_eq: 'h o g e').result.to_sql
#=> "SELECT `issues`.* FROM `issues`
# WHERE REPLACE(`issues`.`name`, ' ', '') = 'hoge'"
Define ransacker
NamedFunctionはDB卆贋しがちなので廣吭
なんかいい圭隈あったら縮えてください
Custom Predicate
config/initializers/ransack.rb で協x
Ransack.configure do |config|
config.add_predicate 'equals_diddly',
arel_predicate: 'eq',
formatter: proc { |v| "#{v}-diddly" },
validator: proc { |v| v.present? },
compounds: true,
type: :string
end
護曄
Conclusion
Conclusion
4 C嬬として碧鮫中が駅勣ならとりあえずransack聞う
4 }jな碧が駅勣になっても伏SQLに毛げない
4 Arelとはちょっと鬚栽う
4 view helperも弼?できる
4 でもorのY栽くらいwhereでできるようになって圀しい

More Related Content

ransack, ransack, ransack