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

Feature/console app search history #6

Merged
merged 2 commits into from
Sep 5, 2023
Merged
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ Once the server is running, open your web browser and go to <http://localhost:30
#### To run console app:

```zsh
ruby my_program new --type education --participants 1 --price_min 0.1 --price_max 30 --accessibility_min 0.1 --accessibility_max 0.5
ruby my_program.rb new --type education --participants 1 --price_min 0.1 --price_max 30 --accessibility_min 0.1 --accessibility_max 0.5
```

```zsh
ruby my_program.rb list
```

Make sure you have local server running to have console app working.
Expand Down
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
include Pundit::Authorization
include Pagy::Backend
end
3 changes: 2 additions & 1 deletion app/interactors/api_wrapper/index/save_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def call

def save_record!
model.create!(
activity_type: activity['activity'],
activity: activity['activity'],
activity_type: activity['type'],
participants: activity['participants'],
price: activity['price'],
link: activity['link'],
Expand Down
4 changes: 2 additions & 2 deletions app/queries/api_wrapper/latest_activities/sort_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SortQuery
DEFAULT_SORT = 'created_at'
DEFAULT_DIRECTION = 'desc'

attr_reader :sort, :order, :scope
attr_reader :scope

def initialize(params, scope)
@sort = params[:sort] || DEFAULT_SORT
Expand All @@ -15,7 +15,7 @@ def initialize(params, scope)
end

def call
scope.order(Arel.sql("#{sort} #{order}"))
scope.order(Arel.sql("#{@sort} #{@order}"))
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion app/views/api_wrapper/latest_activities.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@
%table.table.table-striped
%thead
%tr
%th Activity
%th Type
%th Participants
%th Price
%th Date
%th Link
%th Key
%th Accessibility
%th Created at
%tbody
- @activities.each do |activity|
%tr
%td= activity.activity
%td= activity.activity_type
%td= activity.participants
%td= activity.price
%td= activity.link
%td= activity.key
%td= activity.accessibility
%td= activity.created_at.strftime('%Y-%m-%d %H:%M:%S')
!= pagy_bootstrap_nav @pagy if @pagy && @pagy.pages > 1
- else
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20230901123856_create_activities.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CreateActivities < ActiveRecord::Migration[7.0]
def change
create_table :activities do |t|
t.string :activity
t.string :activity_type
t.integer :participants
t.float :price
Expand Down
1 change: 1 addition & 0 deletions db/schema.rb

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

40 changes: 32 additions & 8 deletions my_program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ class MyProgram < Thor
option :maxaccessibility

def new
params = options.transform_keys(&:to_s)
result = ApiWrapper::Index::Organizer.call(params_to_validate: params)
if result.success?
console_print(result)
else
puts "Error: #{result.errors}"
end
result = ApiWrapper::Index::Organizer.call(params_to_validate: options.transform_keys(&:to_s))
result.success? ? new_print(result) : puts("Error: #{result.errors}")
end

desc 'list', 'List latest activities'

def list
result = ApiWrapper::LatestActivities::Organizer.call(latest_activities_params: { 'order' => 'desc',
'sort' => 'created_at' })
result.success? ? list_print(result.activities.first(5)) : puts("Error: #{result.errors}")
end

private

def console_print(result)
def new_print(result)
pastel = Pastel.new
headers = colorized_headers(pastel)
rows = colorized_rows(pastel, result.activity)
Expand All @@ -37,6 +40,15 @@ def console_print(result)
puts pastel.yellow("\nActivity saved to database")
end

def list_print(activities)
pastel = Pastel.new
headers = colorized_headers(pastel)
rows = activities.map { |activity| colorized_rows_from_model(pastel, activity) }

table = TTY::Table.new headers, rows
puts table.render(:ascii, padding: [0, 1, 0, 1])
end

def colorized_headers(pastel)
%w[Activity Type Participants Price Link Key Accessibility].map { |header| pastel.blue.bold(header) }
end
Expand All @@ -46,6 +58,18 @@ def colorized_rows(pastel, activity)
row = row_data.map { |key| pastel.green(activity[key]) }
[row]
end

def colorized_rows_from_model(pastel, activity)
[
pastel.green(activity.activity),
pastel.green(activity.activity_type),
pastel.green(activity.participants),
pastel.green(activity.price),
pastel.green(activity.link),
pastel.green(activity.key),
pastel.green(activity.accessibility)
]
end
end

MyProgram.start(ARGV)
Loading