Model周り - helpy code reading

共通部分を切り出す

CakePHPでいうBehaviorやComponent, Helper

Rails 共通部分を切り出す (View, Controller, Model) メモ - Qiita

関連したモデルのフォームでの扱い

Rails ネストした関連先のテーブルもまとめて保存する (accepts_nested_attributes_for、fields_for) - Qiita

helpyではtopicが複数のpostを持つ事ができる構造で、topic.rb内で 'accepts_nested_attributes_for :posts' と記述し、

viewでは下記のように定義している。

<%= f.simple_fields_for :posts do |p| %>
      <%= p.input :body, input_html: {:rows => 8, :cols => 60, placeholder: I18n.t(:how_can_we_help), label: 'Message', class: 'disable-empty form-control topic-placeholder'} %>
    <% end %>

model attachments

こういったプラグインはどのMVCフレームワークでも頻繁にお世話になりそう。

GitHub - assembler/attachinary: Attachments handler for Rails that uses Cloudinary for storage.

postgresに特化にした検索用のGem

GitHub - Casecommons/pg_search: pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search

# モデル内で下記ように定義すると
include PgSearch
pg_search_scope :admin_search,
                  against: [:id, :name, :user_name, :current_status, :post_cache]

# 下記のように呼び出せる
 @topics = Topic.admin_search(params[:q]).page params[:page]

polymorphicな関連

helpyではtopicsとpostsに投票機能が付いていて、どちらもvoteモデルの関連で実現されている。

# vote.rb

class Vote < ActiveRecord::Base

  belongs_to :voteable, :polymorphic => true

  ...

# topic.rb
class Topic < ActiveRecord::Base

  ...
  
  has_many :votes, :as => :voteable