Skip to content

Commit

Permalink
introduced institute model
Browse files Browse the repository at this point in the history
includes:
 - institute model
 - world universitites json (public json file)
 - select2 (v4)
  • Loading branch information
biwek committed Mar 25, 2018
1 parent fd31b7d commit 2a8df57
Show file tree
Hide file tree
Showing 20 changed files with 116,172 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

#= require cocoon

#= require select2.min

#= require_tree .
31 changes: 31 additions & 0 deletions app/assets/javascripts/scholars.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
formatItem = (item) ->
if item.loading
"Searching..."
else
'<span class=\'select2-result-repository__title\'>' + item.name + ' ' + '<small class=\'text-muted\'>' + item.country + '</small>' +'</span>'

formatItemSelection = (item) ->
'<span class=\'select2-result-repository__title\'>' + item.name + ' ' + '<small class=\'text-muted\'>' + item.country + '</small>' +'</span>'

$(document).on "ready page:load remote:load turbolinks:load", ->

$(".web_urls").each ->
Expand All @@ -6,3 +15,25 @@ $(document).on "ready page:load remote:load turbolinks:load", ->
$(@).on
"cocoon:before-insert": (e, task_to_be_added) ->
task_to_be_added.fadeIn 300

$("select[name*='scholar[discipline_id]']").select2
placeholder: ""

# $("select[name*='scholar[institute_token]']").select2
$("select.world_universities").select2
minimumInputLength: 3
dataType: 'json'
ajax:
url: "/world_universities"
data: (params) ->
query = {name: params.term}
processResults: (data, params) ->
results: data
escapeMarkup: (markup) -> markup
templateResult: formatItem
templateSelection: formatItemSelection

$("form#scholar-form").submit (ev) ->
uni = $(@).find("select.world_universities").select2("data")[0]
if uni
$(@).find("input[name$='[institute_token]']").val "#{uni.name},#{uni.country}"
4 changes: 4 additions & 0 deletions app/assets/stylesheets/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ p, .alert {
font-size: 14px;
}

.card .card-body {
padding: 0.75em;
}

.site-footer {
font-size: 12px;
margin-top: 40px;
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*= require bootstrap
*= require bootstrap-grid
*= require bootstrap-reboot
*= require select2.min
*/

@import "font-awesome";
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
class PagesController < ApplicationController

def world_universities
# JSON file downloaded from:
# https://github.com/Hipo/university-domains-list
# Opting with this approach as issues with CORS on ajax request
# For updated university list, this file would need to be re-downloaded.
# FYI: last updated datestamp included in the filename
file = File.read("#{Rails.root}/public/assets/25012018_world_universities.json")
unis = JSON.parse(file).each_with_index.map do |uni, i|
uni["id"] = i + 1 # view logic: required for select purposes
uni
end
if name_q = params[:name]
unis = unis.select { |u| u["name"].downcase.include?(name_q) }
end
render json: unis
end

end
6 changes: 5 additions & 1 deletion app/controllers/scholars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
class ScholarsController < ApplicationController

def index
@scholars = Scholar.preload(discipline: :self_and_ancestors)
@scholars = Scholar.preload(:institute,
:web_urls,
discipline: :self_and_ancestors)
.order(updated_at: :desc)
end

def new
Expand All @@ -25,6 +28,7 @@ def scholar_params
:last_name,
:description,
:discipline_id,
:institute_token,
web_urls_attributes: [:id,
:title,
:url,
Expand Down
7 changes: 7 additions & 0 deletions app/models/institute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Institute < ApplicationRecord

def to_s
"#{name}, #{country}"
end

end
9 changes: 9 additions & 0 deletions app/models/scholar.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
class Scholar < ApplicationRecord

belongs_to :discipline
belongs_to :institute, optional: true
has_many :web_urls, as: :linkable

accepts_nested_attributes_for :web_urls, reject_if: :all_blank, allow_destroy: true

validates :first_name, :last_name, :discipline, presence: true

attr_reader :institute_token

def name
[first_name, middle_name, last_name].compact.join(" ")
end

def institute_token=(token)
val = token.split(",")
return if val.blank?
self.institute = Institute.find_or_create_by name: val[0], country: val[1]
end

end
20 changes: 17 additions & 3 deletions app/views/scholars/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@
<%= f.input :last_name %>
</div>
</div>
<%= f.association :discipline, collection: Discipline.leaves %>
<div class="row">
<div class="col-md-6">
<%= f.association :discipline, collection: Discipline.roots,
as: :grouped_select,
group_method: :descendants,
group_label_method: :title %>
</div>
<div class="col-md-6">
<div class="form-group select">
<label class="col-form-label select">Institute</label>
<select class="world_universities form-control"></select>
<%= f.hidden_field :institute_token %>
</div>
</div>
</div>
<%= f.input :description %>
</div>
</div>
Expand All @@ -37,7 +51,7 @@

<div class="clearfix mt-3">
<div class="float-right">
<%= f.button :submit, "Save", class: "btn btn-outline-success btn-sm" %>
<%= link_to "Cancel", scholars_path, class: "btn btn-outline-danger btn-sm" %>
<%= f.button :submit, "Save", class: "btn btn-outline-success" %>
<%= link_to "Cancel", scholars_path, class: "btn btn-outline-danger" %>
</div>
</div>
16 changes: 16 additions & 0 deletions app/views/scholars/_scholar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="card flex-md-row mb-4 box-shadow h-md-250">
<div class="card-body d-flex flex-column align-items-start">
<strong class="d-inline-block mb-2 <%= scholar.discipline.root.code %>"><%= scholar.discipline.root %></strong>
<h5 class="mb-0">
<%= scholar.name %>
</h5>
<div class="mb-1 text-muted small"><%= scholar.discipline %></div>
<div class="mb-1 text-muted small"><%= scholar.institute %></div>
<p class="card-text mb-auto"><%= scholar.description %></p>
<div class="small">
<% scholar.web_urls.each do |web_url| %>
<%= link_to web_url.title, web_url.url, target: "_blank" %>
<% end %>
</div>
</div>
</div>
18 changes: 2 additions & 16 deletions app/views/scholars/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<div class="mb-2">
<h4>
<%= Scholar.name.pluralize %>
<%= link_to fa_icon("user-plus"), new_scholar_path, class: "btn btn-outline-primary float-right", title: "Add Scholar" %>
Expand All @@ -12,21 +12,7 @@
<div class="row">
<% grouped_scholars.compact.each do |scholar| %>
<div class="col-md-4">
<div class="card flex-md-row mb-4 box-shadow h-md-250">
<div class="card-body d-flex flex-column align-items-start">
<strong class="d-inline-block mb-2 <%= scholar.discipline.root.code %>"><%= scholar.discipline.root %></strong>
<h5 class="mb-0">
<%= scholar.name %>
</h5>
<div class="mb-1 text-muted small"><%= scholar.discipline %></div>
<p class="card-text mb-auto"><%= scholar.description %></p>
<div class="small">
<% scholar.web_urls.each do |web_url| %>
<%= link_to web_url.title, web_url.url, target: "_blank" %>
<% end %>
</div>
</div>
</div>
<%= render "scholar", scholar: scholar %>
</div>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/scholars/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

<hr />

<%= simple_form_for setup_scholar(@scholar), html: {class: "small"} do |f| %>
<%= simple_form_for setup_scholar(@scholar), html: {id: "scholar-form", class: "small"} do |f| %>
<%= render "form", f: f %>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
# pages
get "about" => "pages#about"
get "privacy" => "pages#privacy"
get "world_universities" => "pages#world_universities"

end
2 changes: 2 additions & 0 deletions db/migrate/20180311180909_create_scholars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def change
t.string :middle_name
t.string :last_name, null: false
t.text :description

t.timestamps
end
add_reference :scholars, :discipline, foreign_key: {references: :disciplines}
end
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20180324150404_create_institutes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateInstitutes < ActiveRecord::Migration[5.1]
def change
create_table :institutes do |t|
t.string :name, null: false
t.string :country, null: false

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20180324221617_add_institute_id_to_scholar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddInstituteIdToScholar < ActiveRecord::Migration[5.1]
def change
add_reference :scholars, :institute, foreign_key: {references: :institutes}
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180318190338) do
ActiveRecord::Schema.define(version: 20180324221617) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -29,13 +29,24 @@
t.integer "parent_id"
end

create_table "institutes", force: :cascade do |t|
t.string "name", null: false
t.string "country", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "scholars", force: :cascade do |t|
t.string "first_name", null: false
t.string "middle_name"
t.string "last_name", null: false
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "discipline_id"
t.bigint "institute_id"
t.index ["discipline_id"], name: "index_scholars_on_discipline_id"
t.index ["institute_id"], name: "index_scholars_on_institute_id"
end

create_table "web_urls", force: :cascade do |t|
Expand All @@ -47,4 +58,5 @@
end

add_foreign_key "scholars", "disciplines"
add_foreign_key "scholars", "institutes"
end
Loading

0 comments on commit 2a8df57

Please sign in to comment.