when 'twitter' then register_by_twitter! when 'facebook' then register_by_facebook! when 'github' then register_by_github! end Friendship.create!(user: invitation.sender, friend: user) Friendship.create!(user: user, friend: invitation.sender) invitation.joined_at = Time.zone.now invitation.receiver = user invitation.save! end true rescue ActiveRecord::RecordInvalid false end
current_user.minor? && (profane_words_used = profane_words_in(@micropost.content)) current_user.increment(:profanity_count, profane_words_used.size) current_user.save(validate: false) send_parent_notifcation_of_profanity(profane_words_used) flash.now[:error] = <<-MSG.html_safe <p>Profanity: '#{profane_words_used.join(", ")}' not allowed! You've tried to use profanity #{view_context.pluralize(current_user.profanity_count, "time")}! </p><p class="parent-notification">Your parents have been notified!</p> MSG render 'static_pages/home' else if @micropost.save flash[:success] = "Micropost created!" redirect_to root_url else render 'static_pages/home' end end end end
:friends, through: :friendships def create_post_with_notifications!(body) transaction do posts.create!(body: body) friends.each do |friend| friend.notifications.create!("#{name}さんが投稿しました") end end end # 大量の似たようなメソッド end
end def initialize(creator:, body:) @creator = creator @body = body end def create! ActiveRecord::Base.transaction do create_post! create_notifications! end end private attr_reader :creator, :body def create_post! creator.posts.create!(body: body) end def create_notifications! creator.friends.each { |friend| create_notification!(friend) } end def create_notification!(friend) friend.notifications.create!( “#{creator.name}さんが投稿しました" ) end end
:set_post, only: :show before_filter :set_recommendations def index end def show end private def set_posts @posts = post.page(params[:page).per(10) end def set_post @post = post.find(params[:id]) end def set_recommendations @recommendation_categories = Category.recommendations @recommendations = @recommendation_categories.posts end end
def show set_recommendations set_post end private def set_posts @posts = post.page(params[:page).per(10) end def set_post @post = post.find(params[:id]) end def set_recommendations @recommendation_categories = Category.recommendations @recommendations = Recommendations.new(@recommendation_categories) end end
@recommendations = fetch_recommendations(@recommendation_categories) @posts = fetch_posts end def show @recommendation_categories = fetch_recommendation_categories @recommendations = fetch_recommendations(@recommendation_categories) @post = fetch_post end private def fetch_posts post.page(params[:page).per(10) end def fetch_post post.find(params[:id]) end def fetch_recommendation_categories Category.recommendations end def fetch_recommendations(categories) Recommenadtions.new(categories) end end
PostIndexViewModel.new end end class PostIndexViewModel def posts @posts ||= Post.page(params[:page).per(10) end def recommendation_categories @recommendation_categories ||= Category.recommendations end def recommendations @recommendations ||= recommendation_categories.posts end end
params: params).call end def initialize(user:, params:) @user = user @params = params @result = true end def call before_callback unless post.save fail! prepare_new return end after_callback self end def post @post ||= user.post.build(params) end private attr_reader :user, :paramas def before_callback Hoge.prepare_create1 Hoge.prepare_create2 end def after_callback Foo.after Bar.after end def prepare_new Fuga.prepare_new end def fail! @result = false end def post_params params.require(:post).permit(:body) end end
current_user).post end def create service = CreatePostService.call(user: current_user, params: params) if service.success? redirect_to root_path else @post = service.post render :new end end end
:params, optional: true def call if post.save notify_to_admin else fail! end end def post user.posts.build(post_params) end private def notify_to_admin AdminMailer.notify_create_post(post).deliver_later end def post_params params.require(:post).permit(:title, :body) end end