# frozen_string_literal: true module Naming extend ActiveSupport::Concern DEFAULT_NAME = "Default Name" # scope, callback, relation はここに定義 included do before_create :assign_default_name, unless: :name? scope :with_name, -> (names) { where(name: names) } end # class メソッド class_methods do # cattr_reader 使ってもいいですが例なのであえて冗長に書いてます def default_name DEFAULT_NAME end end def assign_default_name self.name
![ActiveSupport::Concernの使い方とテスト(RSpec) - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/a73acdedc7cab0da963055cd92ebae23f1e8948f/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Fogp-background-1150d8b18a7c15795b701a55ae908f94.png%3Fixlib%3Drb-1.2.2%26w%3D1200%26mark%3Dhttps%253A%252F%252Fqiita-user-contents.imgix.net%252F~text%253Fixlib%253Drb-1.2.2%2526w%253D840%2526h%253D380%2526txt%253DActiveSupport%25253A%25253AConcern%2525E3%252581%2525AE%2525E4%2525BD%2525BF%2525E3%252581%252584%2525E6%252596%2525B9%2525E3%252581%2525A8%2525E3%252583%252586%2525E3%252582%2525B9%2525E3%252583%252588%252528RSpec%252529%2526txt-color%253D%252523333%2526txt-font%253DAvenir-Black%2526txt-size%253D54%2526txt-clip%253Dellipsis%2526txt-align%253Dcenter%25252Cmiddle%2526s%253D9322ce1c3c16e0915ac7be91c385fe0d%26mark-align%3Dcenter%252Cmiddle%26blend%3Dhttps%253A%252F%252Fqiita-user-contents.imgix.net%252F~text%253Fixlib%253Drb-1.2.2%2526w%253D840%2526h%253D500%2526txt%253D%252540mmyoji%2526txt-color%253D%252523333%2526txt-font%253DAvenir-Black%2526txt-size%253D45%2526txt-align%253Dright%25252Cbottom%2526s%253D23161f6e0507fe9e9dd6fa0c82c8aa78%26blend-align%3Dcenter%252Cmiddle%26blend-mode%3Dnormal%26s%3Df2108b98096e96dd62aaecde4e28ad62)