Skip to content

Commit

Permalink
Add BackOffice
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed Jun 6, 2017
1 parent f3753bf commit 09979f3
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'pg', '~> 0.18'
gem 'rails', '~> 5.1.1'
gem 'redis', '~>3.2'

gem 'administrate', github: 'thoughtbot/administrate'
gem 'bugsnag'
gem 'countries'
gem 'httparty'
Expand Down
48 changes: 48 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
GIT
remote: git://github.com/thoughtbot/administrate.git
revision: a34ee154d9b7af1a6fe29ce14671f6c66398210c
specs:
administrate (0.7.0)
actionpack (>= 4.2, < 5.2)
actionview (>= 4.2, < 5.2)
activerecord (>= 4.2, < 5.2)
autoprefixer-rails (>= 6.0)
datetime_picker_rails (~> 0.0.7)
jquery-rails (>= 4.0)
kaminari (>= 1.0)
momentjs-rails (~> 2.8)
sass-rails (~> 5.0)
selectize-rails (~> 0.6)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -40,6 +56,8 @@ GEM
tzinfo (~> 1.1)
arel (8.0.0)
ast (2.3.0)
autoprefixer-rails (7.1.1)
execjs
awesome_print (1.7.0)
bugsnag (5.3.2)
builder (3.2.3)
Expand All @@ -53,7 +71,10 @@ GEM
money (~> 6.7)
sixarm_ruby_unaccent (~> 1.1)
unicode_utils (~> 1.4)
datetime_picker_rails (0.0.7)
momentjs-rails (>= 2.8.1)
erubi (1.6.0)
execjs (2.7.0)
ffi (1.9.18)
globalid (0.4.0)
activesupport (>= 4.2.0)
Expand All @@ -69,6 +90,22 @@ GEM
pry-byebug (~> 3.4)
pry-coolline (~> 0.2)
pry-doc (~> 0.6)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
kaminari (1.0.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.0.1)
kaminari-activerecord (= 1.0.1)
kaminari-core (= 1.0.1)
kaminari-actionview (1.0.1)
actionview
kaminari-core (= 1.0.1)
kaminari-activerecord (1.0.1)
activerecord
kaminari-core (= 1.0.1)
kaminari-core (1.0.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -83,6 +120,8 @@ GEM
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.2)
momentjs-rails (2.17.1)
railties (>= 3.1)
monetize (1.4.0)
money (~> 6.7)
money (6.9.0)
Expand Down Expand Up @@ -154,6 +193,14 @@ GEM
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
ruby_dep (1.5.0)
sass (3.4.24)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
selectize-rails (0.12.4)
sixarm_ruby_unaccent (1.1.1)
slim (3.0.8)
temple (>= 0.7.6, < 0.9)
Expand Down Expand Up @@ -192,6 +239,7 @@ PLATFORMS
ruby

DEPENDENCIES
administrate!
bugsnag
byebug
countries
Expand Down
42 changes: 42 additions & 0 deletions app/controllers/admin/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# All Administrate controllers inherit from this `Admin::ApplicationController`,
# making it the ideal place to put authentication logic or other
# before_actions.
#
# If you want to add pagination or other controller-level concerns,
# you're free to overwrite the RESTful controller actions.
module Admin
class ApplicationController < Administrate::ApplicationController
http_basic_authenticate_with(
name: ENV.fetch('ADMIN_NAME', 'admin'),
password: ENV.fetch('ADMIN_PASSWORD', 'admin')
)

# Override this value to specify the number of elements to display at a time
# on index pages. Defaults to 20.
# def records_per_page
# params[:per_page] || 20
# end
def index
page = Administrate::Page::Collection.new(dashboard, order: order)

render locals: {
resources: resources,
search_term: search_term,
page: page,
show_search_bar: show_search_bar?,
}
end

private

def resources
r = Administrate::Search.new(resource_resolver, search_term).run
r = order.apply(r)
r.page(params[:page]).per(records_per_page)
end

def search_term
params[:search].to_s.strip
end
end
end
28 changes: 28 additions & 0 deletions app/controllers/admin/games_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Admin
class GamesController < Admin::ApplicationController
# To customize the behavior of this controller,
# you can overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Game.
# page(params[:page]).
# per(10)
# end

# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Game.find_by!(slug: param)
# end

# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information

private

def resources
r = Administrate::Search.new(resource_resolver, search_term).run
r.order_by_title.order_by_region.page(params[:page]).per(records_per_page)
end
end
end
21 changes: 21 additions & 0 deletions app/controllers/admin/prices_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Admin
class PricesController < Admin::ApplicationController
# To customize the behavior of this controller,
# you can overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Price.
# page(params[:page]).
# per(10)
# end

# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Price.find_by!(slug: param)
# end

# See https://administrate-prototype.herokuapp.com/customizing_controller_actions
# for more information
end
end
74 changes: 74 additions & 0 deletions app/dashboards/game_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'administrate/base_dashboard'

class GameDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
prices: Field::HasMany.with_options(
limit: 50
),
id: Field::Number,
raw_game_code: Field::String,
game_code: Field::String,
nsuid: Field::String,
region: Field::Select.with_options(
collection: %w[americas asia europe]
),
title: Field::String,
release_date: Field::DateTime,
cover_url: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
title
game_code
region
nsuid
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
raw_game_code
game_code
nsuid
region
title
release_date
cover_url
prices
created_at
updated_at
].freeze

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
raw_game_code
game_code
nsuid
region
title
release_date
cover_url
].freeze

# Overwrite this method to customize how games are displayed
# across all pages of the admin dashboard.

def display_resource(game)
game.title
end
end
63 changes: 63 additions & 0 deletions app/dashboards/price_dashboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "administrate/base_dashboard"

class PriceDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
game: Field::BelongsTo,
id: Field::Number,
nsuid: Field::String,
country: CountryField,
status: Field::String,
value: MoneyField,
currency: Field::String,
value_in_cents: Field::Number,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
game
nsuid
country
value
].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
game
nsuid
country
status
value
].freeze

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
game
nsuid
country
status
currency
value_in_cents
].freeze

# Overwrite this method to customize how prices are displayed
# across all pages of the admin dashboard.
#
# def display_resource(price)
# "Price ##{price.id}"
# end
end
7 changes: 7 additions & 0 deletions app/fields/country_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'administrate/field/base'

class CountryField < Administrate::Field::Base
def to_s
ISO3166::Country[data].unofficial_names.first
end
end
7 changes: 7 additions & 0 deletions app/fields/money_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'administrate/field/base'

class MoneyField < Administrate::Field::Base
def format
data.format
end
end
6 changes: 6 additions & 0 deletions app/views/fields/country_field/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.text_field field.attribute %>
</div>
1 change: 1 addition & 0 deletions app/views/fields/country_field/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
1 change: 1 addition & 0 deletions app/views/fields/country_field/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
1 change: 1 addition & 0 deletions app/views/fields/money_field/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.format %>
1 change: 1 addition & 0 deletions app/views/fields/money_field/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.format %>
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
Rails.application.routes.draw do
namespace :admin do
resources :games
resources :prices

root to: 'games#index'
end

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

get :best_deals, to: 'application#best_deals'
Expand Down

0 comments on commit 09979f3

Please sign in to comment.