Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Last active November 1, 2019 07:14
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/b1e8723931f4b107b56fad3b297294b9 to your computer and use it in GitHub Desktop.
Save sergio-fry/b1e8723931f4b107b56fad3b297294b9 to your computer and use it in GitHub Desktop.
clean arch examples
#############################################
# Example 1
BookRepository.new.where(author_id: 23).order(:published_at).limit(8)
#############################################
# Example 2
book.to_json
book.to_xml
#############################################
# Example 3
<ul>
<% User.find(order: 'last_name').each do |user| -%>
<li><%= user.last_name %> <%= user.first_name %></li>
<% end %>
</ul>
#############################################
# Example 4
class Book < ActiveRecord
def to_param
"#{book.id}-#{slug}"
end
end
<%= link_to book.title, book %>
#############################################
# Example 5
Class User < ActiveRecord
def full_name
“#{first_name} #{last_name}”
end
end
Name: <%= @user.full_name %>
#############################################
# Example 6
# POST /comments
def create
@post = Post.find params[:post_id]
@user = User.find_or_initialize_by user_params
@user.password = SecureRandom.hex if @user.new_record?
@comment = @post.comments.new(comment_params.merge(user: @user))
flag_spammer
if !verify_recaptcha
redirect_to smart_post_path(@post), error: t('security.robot_atack')
elsif cooldown_spammer?(@user)
redirect_to smart_post_path(@post), error: t('security.spammer_atack')
else
if @user.save && @comment.save
ga_event category: :comments, action: :create, label: @post.title, interaction: 1, value: 1
EventTracker.notify 'comments', 'create', <<-MSG
#{@user.name}:
#{@comment.comment}
#{@post.title} #{(begin
comment_url(@comment)
rescue
:cant_get_comment_url
end)}
MSG
begin
sign_in @user
remember_me @user
rescue
nil
end
@user.follow(@post) if params[:subscribe] == '1'
notify_post_subscribers
redirect_to comment_path(@comment), notice: t('comments.thank_you')
else
redirect_to smart_post_path(@post)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment