Created
March 18, 2020 12:44
-
-
Save Mariusio/1f1acd62f1694523bcf8e38a25cbfb06 to your computer and use it in GitHub Desktop.
Pexels - Applicant Qualification Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Question 2.1 | |
1. In the show action of PostsController we create a new instance variable called "@post" and use the ActiveRecord ORM to generate a SQL query for our database that returns a collection of all entries in the "posts" table that have attribute "published = true" and where the "id" attribute from the "params" hash matches the "id" of the "post" entry. | |
We then use the ORMs "first" method to select the first object from the collection. | |
In the next line we check if "@post" is undefined or nil. If it is we set "@post" to the newest / latest entry in the database. | |
In the "posts/show" view we iterate over the "comments" for a "post" (usually identified by "post_id" attribute on "comment"), insert a paragraph for each comment and fetch and display the "user.name" and the "text" of the "comment". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Question 2.2.1 | |
# change | |
class PostsController < ApplicationController | |
def show | |
@post = Post.where("published = true").where("id = #{params[:id]}").first | |
@post ||= Post.last | |
end | |
end | |
# to | |
class PostsController < ApplicationController | |
def show | |
@post = Post.where(published: true).find_by(id: params[:id]) || Post.last | |
end | |
end | |
# I would change the code as shown above to | |
# - avoid SQL injection attackes as described here: https://www.netsparker.com/blog/web-security/sql-injection-ruby-on-rails-development/ | |
# - make it more concise | |
# - only return single post for a specific id - for all DBs that I worked with so far the id column had to be unique |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%# Question 2.2.2 %> | |
<%# change %> | |
<% @post.comments.each do |comment| %> | |
<p> | |
<b><%= comment.user.name %> wrote a comment:</b> | |
<%= comment.text %> | |
</p> | |
<% end %> | |
<%# to %> | |
<% @post.comments.each do |comment| %> | |
<p> | |
<b><%= comment&.user&.name %> wrote a comment:</b> | |
<%= comment&.text %> | |
</p> | |
<% end %> | |
<%# I would change the code as shown above to avoid getting errors if either "user" is "nil" or "name" is "nil". The Safe Navigation Operator was introduced with ruby 2.3.0, if an older ruby is used we should probably check with <% if comment.user && comment.user.name %> %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returns the input string in reverse order | |
# | |
# Examples: | |
# reverse_string('photography') => "yhpargotohp" | |
# reverse_string('super photography') => "yhpargotohp repus" | |
def reverse_string(input) | |
# verify that input parameter is a String | |
raise ArgumentError.new("Please provide a String") if !input.is_a? String | |
# verify that input parameter is not empty | |
raise ArgumentError.new("Input may not be empty") if input.empty? | |
# iterate over each character of input string and | |
# prepend to input_reverse, thereby reversing the string | |
input_reverse = '' | |
input.each_char { |c| input_reverse.prepend(c) } | |
input_reverse | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require_relative 'reverse_string' | |
class TestReverseString < MiniTest::Unit::TestCase | |
def test_reverse_string | |
assert_equal "yhpargotohp", reverse_string('photography') | |
end | |
def test_reverse_string_with_spaces | |
assert_equal "yhpargotohp repus", reverse_string('super photography') | |
end | |
def test_argument_with_non_string_types | |
assert_raises ArgumentError do | |
reverse_string(1) | |
end | |
end | |
def test_empty_string | |
assert_raises ArgumentError do | |
reverse_string('') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment