Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruby on rails app #31

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions service/ruby-on-rails/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.

# Mark the database schema as having been generated.
db/schema.rb linguist-generated

# Mark any vendored files as having been vendored.
vendor/* linguist-vendored
23 changes: 23 additions & 0 deletions service/ruby-on-rails/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
.bundle

# Ignore all logfiles and tempfiles.
log/*
tmp/*
!log/.keep
!tmp/.keep

# Ignore pidfiles, but keep the directory.
tmp/pids/*
!tmp/pids/
!tmp/pids/.keep


# Ignore master key for decrypting credentials and more.
config/master.key
1 change: 1 addition & 0 deletions service/ruby-on-rails/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
14 changes: 14 additions & 0 deletions service/ruby-on-rails/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM ruby:3.2.2-slim

ENV PORT=8080

RUN apt-get update -qq && apt-get install -y build-essential git pkg-config libpq-dev postgresql-client

WORKDIR /workdir
COPY Gemfile Gemfile.lock /workdir/
RUN bundle install
COPY . /workdir

ENTRYPOINT ["/workdir/bin/docker-entrypoint"]

CMD ["/workdir/bin/rails", "server", "-b", "0.0.0.0"]
12 changes: 12 additions & 0 deletions service/ruby-on-rails/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source "https://rubygems.org"

ruby "3.2.2"

gem "bootsnap", require: false
gem "rails", "~> 7.0.4", ">= 7.0.4.3"
gem "pg", "~> 1.1"
gem "puma", "~> 5.0"

group :development, :test do
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end
170 changes: 170 additions & 0 deletions service/ruby-on-rails/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.4.3)
actionpack (= 7.0.4.3)
activesupport (= 7.0.4.3)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.4.3)
actionpack (= 7.0.4.3)
activejob (= 7.0.4.3)
activerecord (= 7.0.4.3)
activestorage (= 7.0.4.3)
activesupport (= 7.0.4.3)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.4.3)
actionpack (= 7.0.4.3)
actionview (= 7.0.4.3)
activejob (= 7.0.4.3)
activesupport (= 7.0.4.3)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.4.3)
actionview (= 7.0.4.3)
activesupport (= 7.0.4.3)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.4.3)
actionpack (= 7.0.4.3)
activerecord (= 7.0.4.3)
activestorage (= 7.0.4.3)
activesupport (= 7.0.4.3)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.4.3)
activesupport (= 7.0.4.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.4.3)
activesupport (= 7.0.4.3)
globalid (>= 0.3.6)
activemodel (7.0.4.3)
activesupport (= 7.0.4.3)
activerecord (7.0.4.3)
activemodel (= 7.0.4.3)
activesupport (= 7.0.4.3)
activestorage (7.0.4.3)
actionpack (= 7.0.4.3)
activejob (= 7.0.4.3)
activerecord (= 7.0.4.3)
activesupport (= 7.0.4.3)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.4.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
bootsnap (1.16.0)
msgpack (~> 1.2)
builder (3.2.4)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
erubi (1.12.0)
globalid (1.1.0)
activesupport (>= 5.0)
i18n (1.13.0)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
irb (1.6.4)
reline (>= 0.3.0)
loofah (2.21.3)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.18.0)
msgpack (1.7.1)
net-imap (0.3.4)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
timeout
net-smtp (0.3.3)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.1-arm64-darwin)
racc (~> 1.4)
pg (1.5.3)
puma (5.6.5)
nio4r (~> 2.0)
racc (1.6.2)
rack (2.2.7)
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.4.3)
actioncable (= 7.0.4.3)
actionmailbox (= 7.0.4.3)
actionmailer (= 7.0.4.3)
actionpack (= 7.0.4.3)
actiontext (= 7.0.4.3)
actionview (= 7.0.4.3)
activejob (= 7.0.4.3)
activemodel (= 7.0.4.3)
activerecord (= 7.0.4.3)
activestorage (= 7.0.4.3)
activesupport (= 7.0.4.3)
bundler (>= 1.15.0)
railties (= 7.0.4.3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
railties (7.0.4.3)
actionpack (= 7.0.4.3)
activesupport (= 7.0.4.3)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.0.6)
reline (0.3.4)
io-console (~> 0.5)
thor (1.2.2)
timeout (0.3.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.8)

PLATFORMS
arm64-darwin-22

DEPENDENCIES
bootsnap
debug
pg (~> 1.1)
puma (~> 5.0)
rails (~> 7.0.4, >= 7.0.4.3)

RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.4.13
60 changes: 60 additions & 0 deletions service/ruby-on-rails/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Ruby on Rails - Eco Benchmark

## Context

https://github.com/Boavizta/ecobenchmark-applicationweb-backend

## Application

### Generation

```
rails new ecobenchmark \
--api \
--database=postgresql \
--skip-action-mailer \
--skip-action-mailbox \
--skip-action-text \
--skip-active-job \
--skip-active-storage \
--skip-action-cable \
--skip-asset-pipeline \
--skip-javascript \
--skip-hotwire \
--skip-test \
--skip-system-test
```

### Specifities

#### Creation timestamp

To use `creation_date` as the creation timestamp instead of the Rails-default `created_at`, we set an alias in `app/models/application_record.rb`

```rb
class ApplicationRecord < ActiveRecord::Base
# ...

alias_attribute :created_at, :creation_date
end
```

#### Initial schema

Because database tables are already defined, we create the database schema with a database migration file present in the `db/migrations` folder.

The `if_not_exists` prevent the application to override the existing database.

#### Docker-Composer hostname

On the Docker-Compose network, the app is named "service", so the runner will make requests to "http://service:8080".

By default, Rails blocks unknown hosts, so we need to add it to the configuration, in `config/environments/development.rb`.

#### SQL requests implementation

To respect the contribution guidelines, we use custom classes (in `app/lib` folder) to generate the responses for the GET requests.

As for the POST requests, the SQL requests generated by the ORM are pretty much the same, the only minor difference is that the `id` attribute is not present in the INSERT INTO as an input, but as the output of the request.

Thus, the request `INSERT INTO account(id, login, creation_date) values ($1, $2, $3)` becomes `INSERT INTO account(login, creation_date) values ($2, $3) RETURNING id`.
6 changes: 6 additions & 0 deletions service/ruby-on-rails/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative "config/application"

Rails.application.load_tasks
10 changes: 10 additions & 0 deletions service/ruby-on-rails/app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AccountsController < ApplicationController
def create
@account = Account.new(login: params[:login])
if @account.save
render json: @account, status: :created
else
render json: @account.errors, status: :bad_request
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationController < ActionController::API
end
Empty file.
5 changes: 5 additions & 0 deletions service/ruby-on-rails/app/controllers/health_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HealthController < ApplicationController
def check
head :no_content
end
end
23 changes: 23 additions & 0 deletions service/ruby-on-rails/app/controllers/lists_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

class ListsController < ApplicationController
before_action :load_account

def index
render json: ListResponse.generate(@account.id, params[:page])
end

def create
@list = @account.lists.new(params.permit(:name))
if @list.save
render json: @list, status: :created
else
render json: @list.errors, status: :bad_request
end
end

private

def load_account
@account = Account.find(params[:account_id])
end
end
5 changes: 5 additions & 0 deletions service/ruby-on-rails/app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class StatsController < ApplicationController
def index
render json: StatResponse.generate
end
end
19 changes: 19 additions & 0 deletions service/ruby-on-rails/app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

class TasksController < ApplicationController
before_action :load_list

def create
@task = @list.tasks.new(params.permit(:name, :description))
if @task.save
render json: @task, status: :created
else
render json: @task.errors, status: :bad_request
end
end

private

def load_list
@list = List.find(params[:list_id])
end
end
Loading