Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a14160a
add devise gem configuration
evans22j Aug 2, 2022
c4c3ab2
update styles
evans22j Aug 2, 2022
26aca8b
add user devise config in application controller
evans22j Aug 2, 2022
50e2873
create new html file in devise folder
evans22j Aug 2, 2022
78ea350
Update new.html.erb in registrations and sessions
evans22j Aug 2, 2022
79d7be3
Update application.erb in views/layouts
evans22j Aug 2, 2022
3ee66aa
Run rails db:migrate, and update index and shot html file in views/users
evans22j Aug 2, 2022
25cd143
add confirmable
evans22j Aug 2, 2022
5f420cf
add devise gem configuration
evans22j Aug 2, 2022
5c29be8
Run rails db:migrate, and update index and shot html file in views/users
evans22j Aug 2, 2022
52ffcd4
add user devise config in application controller
evans22j Aug 2, 2022
9e74141
resolve linter errors
evans22j Aug 2, 2022
5820d15
Update comments_controller.rb and create destroy method
evans22j Aug 3, 2022
1612884
Update post_controller.rb and create destroy method
evans22j Aug 3, 2022
8670658
add cancancan authorization
evans22j Aug 3, 2022
e58a588
Update comment.rb and create update_comments_counter_on_destroy method
evans22j Aug 3, 2022
b468531
Update post.rb
evans22j Aug 3, 2022
2779baf
Update user.rb
evans22j Aug 3, 2022
fba2715
Update posts show.html.erb to include delete option
evans22j Aug 3, 2022
d0a59b3
Update users show.html.erb
evans22j Aug 3, 2022
fe51071
Update routes.rb to include destroy action
evans22j Aug 3, 2022
a8cb60b
add cancancan authorization
evans22j Aug 3, 2022
74ec27d
add cancancan authorization
evans22j Aug 3, 2022
68ff6cf
correct linter errors
evans22j Aug 3, 2022
7f5e080
Create users_controller.rb in app/controllers/api/v1
evans22j Aug 4, 2022
ad24efc
Update comments_controller.rb in app/api/v1
evans22j Aug 4, 2022
c255cae
Update postscontroller.rb in app/api/v1
evans22j Aug 4, 2022
88f8c87
Update userscontroller.rb
evans22j Aug 4, 2022
63a8d0b
Create exception_handler.rb module in app/controllers/concerns
evans22j Aug 4, 2022
9babb07
Create response.rb module in app/controllers/concerns
evans22j Aug 4, 2022
5b58ba7
update files
evans22j Aug 4, 2022
85b802c
Create json_web_token.rb in app/lib
evans22j Aug 4, 2022
678209d
Add jwt gem in gemfile
evans22j Aug 4, 2022
e9fa22f
Edit Linters
Aug 5, 2022
0556a61
Change DB Password Config
Aug 6, 2022
721d16b
Added Devise Model
Aug 6, 2022
9a03452
Migrate DB
Aug 6, 2022
2cdef9c
Delete Devise Views
Aug 6, 2022
5f0fce3
Added Devise Views
Aug 6, 2022
231363b
Edit Devise Registration View
Aug 6, 2022
cf02c5e
Edit README.md
Aug 6, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .idea/blog-app.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codestyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ gem 'tzinfo-data'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false

gem 'devise'

gem 'cancancan'

gem 'jwt'

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem 'ffi'
gem 'ffi', github: 'ffi/ffi', submodules: true

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'rails-controller-testing'
Expand Down
33 changes: 28 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: https://github.com/ffi/ffi.git
revision: 07a1c0c028942f786b80f48206795a8fa6b0e8f5
submodules: true
specs:
ffi (1.15.5)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -69,13 +76,15 @@ GEM
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
bcrypt (3.1.18)
bindex (0.8.1)
bootsnap (1.12.0)
msgpack (~> 1.2)
builder (3.2.4)
bullet (7.0.2)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
cancancan (3.4.0)
capybara (3.37.1)
addressable
matrix
Expand All @@ -88,11 +97,15 @@ GEM
childprocess (4.1.0)
concurrent-ruby (1.1.10)
crass (1.0.6)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
diff-lcs (1.5.0)
digest (3.1.0)
erubi (1.10.0)
ffi (1.15.5)
ffi (1.15.5-x64-mingw-ucrt)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.12.0)
Expand All @@ -104,6 +117,7 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.2)
jwt (2.4.1)
loofah (2.18.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -134,8 +148,9 @@ GEM
racc (~> 1.4)
nokogiri (1.13.7-x64-mingw-ucrt)
racc (~> 1.4)
nokogiri (1.13.7-x86_64-linux)
nokogiri (1.13.7-x86_64-darwin)
racc (~> 1.4)
orm_adapter (0.5.0)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -181,6 +196,9 @@ GEM
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.5.0)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rexml (3.2.5)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
Expand Down Expand Up @@ -240,6 +258,8 @@ GEM
tzinfo (>= 1.0.0)
unicode-display_width (2.2.0)
uniform_notifier (1.16.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.0)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand All @@ -260,15 +280,18 @@ GEM
PLATFORMS
arm64-darwin-21
x64-mingw-ucrt
x86_64-linux
x86_64-darwin-21

DEPENDENCIES
bootsnap
bullet
cancancan
capybara
ffi
devise
ffi!
importmap-rails
jbuilder
jwt
pg (~> 1.1)
puma (~> 5.0)
rails (~> 7.0.3)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ This project is build with:
- Twitter: [@Evans_22J](https://twitter.com/Evans_22J)
- LinkedIn: [@Evans Sitibekiso](https://www.linkedin.com/in/evans-sitibekiso/)

👤 **Edward Odhiambo**

- GitHub: [@whit3d](https://github.com/white3d)
- Twitter: [@odhiambo_ed](https://twitter.com/odhiambo_ed)
- LinkedIn: [Edward Odhiambo](https://www.linkedin.com/in/edward-odhiambo-6a462a21b/)

## 🤝 Contributor


Expand Down
2 changes: 0 additions & 2 deletions app/assets/stylesheets/user_style.css
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ a {
display: flex;
justify-content: center;
align-items: center;

/* text-align: center; */
}

.text-button {
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Api::V1::PostsController < ApplicationController
def index
@posts = Post.where(author_id: params[:author_id])
if @posts.empty?
json_response({ msg: 'no post or user found' }, 400)
else
render json: @posts
end
end
end
30 changes: 30 additions & 0 deletions app/controllers/api/v1/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Api::V1::CommentsController < ApplicationController
before_action :authorize_request
protect_from_forgery with: :null_session

def index
@comments = Comment.where(post_id: params[:post_id])

if @comments.empty?
json_response({ msg: 'No post, comments or user found' }, 400)
else
render json: @comments
end
end

def create
@comment = Comment.create(
text: comment_params[:text],
author_id: params[:author_id],
post_id: params[:post_id]
)

json_response(@comment, :created)
end

private

def comment_params
params.require(:comment).permit(:text)
end
end
11 changes: 11 additions & 0 deletions app/controllers/api/v1/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::V1::PostsController < ApplicationController
before_action :authorize_request
def index
@posts = Post.where(author_id: params[:author_id])
if @posts.empty?
json_response({ msg: 'no post or user found' }, 400)
else
render json: @posts
end
end
end
7 changes: 7 additions & 0 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Api::V1::UsersController < ApplicationController
before_action :authorize_request
def index
@users = User.all
render json: @users, status: :ok
end
Comment on lines +3 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Devise expects an email and password for authentication or authorization, but the user's table doesn't have an email. Kindly fix the error that comes when you want to log in.

email

end
29 changes: 27 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
class ApplicationController < ActionController::Base
def current_user
User.first
include Response
include ExceptionHandler

before_action :authenticate_user!
protect_from_forgery with: :exception
before_action :update_allowed_parameters, if: :devise_controller?

protected

def update_allowed_parameters
devise_parameter_sanitizer.permit(:sign_up) do |field|
field.permit(:name, :photo, :bio, :posts_counter, :email, :password, :password_confirmation)
end
end

def authorize_request
header = request.headers['Auth']
header = header.split.last if header
begin
@decoded = JsonWebToken.decode(header)
p @decoded
@current_user = User.find(@decoded['id'])
rescue ActiveRecord::RecordNotFound => e
render json: { errors: e.message }, status: :unauthorized
rescue JWT::DecodeError => e
render json: { error: e.message }, status: :unauthorized
end
end
end
15 changes: 15 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class CommentsController < ApplicationController
def index
@user = current_user
@comments = @user.comments

respond_to do |format|
format.json { render json: @comments }
end
end

def new
@comment = Comment.new
@user = current_user
Expand All @@ -21,6 +30,12 @@ def create
end
end

def destroy
comment = Comment.find(params[:id])
comment.destroy
redirect_to user_post_url(current_user, comment.post), notice: 'Comment was successfully destroyed.'
end

private

def comment_params
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/concerns/exception_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ExceptionHandler
# provides the more graceful `included` method
extend ActiveSupport::Concern

included do
rescue_from ActiveRecord::RecordNotFound do |e|
json_response({ message: e.message }, :not_found)
end

rescue_from ActiveRecord::RecordInvalid do |e|
json_response({ message: e.message }, :unprocessable_entity)
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/concerns/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Response
def json_response(object, status = :ok)
render json: object, status:
end
end
Loading