Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Last active October 30, 2020 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergio-fry/b08c3d2ced928fc9bc078f08d40b14b0 to your computer and use it in GitHub Desktop.
Save sergio-fry/b08c3d2ced928fc9bc078f08d40b14b0 to your computer and use it in GitHub Desktop.
Code example
class User < ApplicationRecord
has_many :posts
after_create :notify_moderator_by_sms_about_new_user
def deactivate!
puts "User deactivation started"
notify_moderator_by_sms_about_user_deactivation
self.update_attribute :role, :disabled
posts.each do |post|
post.update_column :published, false
end
puts "User deactivation finished"
end
def admin?
if role = :admin
true
else
false
end
end
def to_json
attributes.merge(full_name: full_name).to_json
end
def full_name
[first_name, last_name].join(" ")
end
private
def notify_moderator_by_sms_about_new_user
uri = URI('http://sms-sender-gateway.local/api')
res = Net::HTTP.post_form(
uri,
phone: "+79161234567",
text: "New user ID: #{id}, Name: #{full_name}"
)
end
def notify_moderator_by_sms_about_user_deactivation
uri = URI('http://sms-sender-gateway.local/api')
res = Net::HTTP.post_form(
uri,
phone: "+79161234567",
text: "User deactivated ID: #{id}, Name: #{full_name}"
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment