Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into 1643_hide_unavailable_items
Browse files Browse the repository at this point in the history
  • Loading branch information
yongjie-lin authored Mar 3, 2017
2 parents 08f5c88 + a50e6f4 commit 9ff6aa1
Show file tree
Hide file tree
Showing 25 changed files with 436 additions and 30 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# 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.
Expand Down
9 changes: 9 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ $(document).ready(function() {
]
});

$('.datatable-order').dataTable({
"pagingType": "full_numbers",
"scrollX": false,
"order": [[9,"asc"]],
"columnDefs": [
{ "orderable": false, "targets": [ "no_sort" ] }
]
});

// For DataTables in Bootstrap tabs
// see https://datatables.net/examples/api/tabs_and_scrolling.html
$('a[data-toggle="tab"]').on( 'shown.bs.tab', function (e) {
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/equipment_models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def new
end

def create
equipment_model_params[:ordering] ||= EquipmentModel.count
@equipment_model = EquipmentModel.new(equipment_model_params)
if @equipment_model.save
flash[:notice] = 'Successfully created equipment model.'
Expand Down Expand Up @@ -113,6 +114,20 @@ def update
end
end

def up
OrderingHelper.new(@equipment_model).up
redirect_to request.referer
end

def down
OrderingHelper.new(@equipment_model).down
redirect_to request.referer
end

def verify_order
OrderingHelper.new(@equipment_model).verify_order
end

def deactivate
if params[:deactivation_cancelled]
flash[:notice] = 'Deactivation cancelled.'
Expand All @@ -123,6 +138,7 @@ def deactivate
.save(validate: false)
end
super
OrderingHelper.new(@equipment_model).deactivate_order
else
flash[:error] = 'Oops, something went wrong.'
redirect_to @equipment_model
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/equipment_models_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ def evaluate_img_presence(equipment_model)
'no-image-260.gif'
end
end

def display_order(em)
em.category_ordering.index(em.ordering) + 1
end
end
12 changes: 12 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ class Category < ActiveRecord::Base
# 'includes'
scope :active, ->() { where("#{table_name}.deleted_at is null") }

def active_models
equipment_models.where(deleted_at: nil)
end

def active_model_count
active_models.count
end

def inactive_models
equipment_models.where.not(deleted_at: nil)
end

def maximum_per_user
max_per_user || Float::INFINITY
end
Expand Down
24 changes: 23 additions & 1 deletion app/models/equipment_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ class EquipmentModel < ActiveRecord::Base

validates :name,
:description,
:ordering,
:category, presence: true
validates :name, uniqueness: true
validates :name,
:ordering, uniqueness: true
validates :late_fee, :replacement_fee,
numericality: { greater_than_or_equal_to: 0 }
validates :ordering, numericality: { greater_than_or_equal_to: -1,
only_integer: true }

validates :max_per_user,
:max_checkout_length, numericality: { allow_nil: true, \
only_integer: true, \
Expand Down Expand Up @@ -121,6 +126,8 @@ def not_associated_with_self
attachment.instance.normalized_photo_name
end

after_create :assign_ordering, unless: ->(em) { em.ordering }

def normalized_photo_name
"#{id}-#{photo_file_name.gsub(/[^a-zA-Z0-9_\.]/, '_')}"
end
Expand Down Expand Up @@ -151,6 +158,15 @@ def maximum_renewal_days_before_due
renewal_days_before_due || category.maximum_renewal_days_before_due
end

def active_in_category
category.active_models
end

def category_ordering
EquipmentModel.where(category: category)
.map(&:ordering).sort
end

def active_reservations
if AppConfig.check :requests_affect_availability
reservations.not_overdue.active_or_requested
Expand Down Expand Up @@ -212,4 +228,10 @@ def available_item_select_options
end\
.join.html_safe
end

private

def assign_ordering
update_attribute(:ordering, id)
end
end
5 changes: 2 additions & 3 deletions app/views/catalog/_catalog_listing.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
<%= link_to category.name, category_equipment_models_path(category) %>
</h2>
<div class="thumbnails row">
<% equipment_models.each do |equipment_model| %>
<% (equipment_models[0, 3]).each do |equipment_model| %>
<div class="col-md-4">
<%= render partial: 'catalog/equipment_model_div', locals: {
equipment_model: equipment_model,
availability_hash: availability_hash,
available_string: available_string,
qualifications_hash: qualifications_hash
} %>
qualifications_hash: qualifications_hash} %>
</div>
<% end %>
</div>
Expand Down
10 changes: 8 additions & 2 deletions app/views/equipment_models/_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<table id="table_equipment_models" class="datatable-wide table table-striped table-bordered">
<table id="table_equipment_models" class="datatable-order table table-striped table-bordered">
<thead>
<tr>
<th class="text-center col-md-2">Name</th>
Expand All @@ -12,6 +12,9 @@
<% if can? :manage, EquipmentModel %>
<th class="no_sort text-center"> </th>
<th class="no_sort text-center"> </th>
<% unless @category.blank? %>
<th class="text-center">Ordering</th>
<%end %>
<% end%>
<% end %>
</tr>
Expand All @@ -36,9 +39,12 @@
<% if can? :manage, EquipmentModel %>
<td class="text-center"><%= link_to "Edit", edit_equipment_model_path(equipment_model), class: "btn btn-default" %></td>
<td class="text-center"><%= equipment_model.decorate.make_deactivate_btn %></td>
<% unless @category.blank? %>
<td class="text-center"><%= display_order(equipment_model) %><%= link_to '', sort_up_path(equipment_model), class: "btn glyphicon glyphicon-arrow-up" , method: :put%><%= link_to "", sort_down_path(equipment_model), class: "btn glyphicon glyphicon-arrow-down" , method: :put%></td>
<% end %>
<% end%>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</table>
2 changes: 1 addition & 1 deletion app/views/layouts/_content_with_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="content" class="col-md-9">
<div id="content-with-sidebar" class="col-md-9">
<% if show_title? %>
<div class="page-header">
<h1>
Expand Down
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@

get 'status/index'

put '/equipment_models/:id/up' => 'equipment_models#up',
as: 'sort_up'
put '/equipment_models/:id/down' => 'equipment_models#down',
as: 'sort_down'
# generalized matcher
match ':controller(/:action(/:id(.:format)))', via: [:get, :post, :put,
:delete]
Expand Down
24 changes: 24 additions & 0 deletions db/migrate/20160329015527_add_ordering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class AddOrdering < ActiveRecord::Migration
def up
return if column_exists?(:equipment_models, :ordering)

add_column :equipment_models, :ordering, :integer, :null => false

# store ActiveRecord connection to run queries
conn = ActiveRecord::Base.connection

# go through all categories
models = conn.exec_query('SELECT * FROM equipment_models WHERE '\
'active = 1')
ord = 1
models.each do |m|
conn.execute("UPDATE equipment_models SET ordering = "\
"#{ord} WHERE id = #{m['id']}")
ord += 1
end
end
def down
remove_column :equipment_models, :ordering
end
end

1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
t.integer "equipment_items_count", limit: 4, default: 0, null: false
t.decimal "late_fee_max", precision: 10, scale: 2, default: 0.0
t.integer "overdue_count", limit: 4, default: 0, null: false
t.integer "ordering", limit: 4, null: false
end

create_table "equipment_models_associated_equipment_models", id: false, force: :cascade do |t|
Expand Down
84 changes: 84 additions & 0 deletions lib/ordering_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# frozen_string_literal: true
class OrderingHelper
attr_reader :eq_mod

def initialize(eq_mod)
@eq_mod = eq_mod
end

def up
return unless ordering > cat_first
new_ordering = rel_predecessor(ordering)
swap(predecessor, ordering, new_ordering)
end

def down
return unless ordering < cat_last
new_ordering = rel_successor(ordering)
swap(successor, ordering, new_ordering)
end

def deactivate_order
current_last = cat_last
ms = successors
ms.each do |m|
m.update_attribute('ordering', rel_predecessor(m.ordering))
end
eq_mod.update_attribute('ordering', current_last)
end

private

delegate :ordering, :category_id, to: :eq_mod

def rel_ordering
abs_to_rel(eq_mod.ordering)
end

def category_orderings
@category_ordering ||= @eq_mod.category_ordering
end

def cat_last
category_orderings[-1]
end

def cat_first
category_orderings[0]
end

def abs_to_rel(n)
category_orderings.index(n)
end

def rel_to_abs(n)
category_orderings[n]
end

def rel_successor(n)
rel_to_abs(abs_to_rel(n) + 1)
end

def rel_predecessor(n)
rel_to_abs(abs_to_rel(n) - 1)
end

def successor
@successor ||= EquipmentModel.where(ordering: rel_successor(ordering),
deleted_at: nil).first
end

def predecessor
@predecessor ||= EquipmentModel.where(ordering: rel_predecessor(ordering),
deleted_at: nil).first
end

def swap(target, old_ordering, new_ordering)
target.update_attribute('ordering', old_ordering)
eq_mod.update_attribute('ordering', new_ordering)
end

def successors
EquipmentModel.where('ordering > ?', ordering)
end
end
1 change: 1 addition & 0 deletions lib/seed/equipment_model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def self.generate
em.renewal_days_before_due = rand(0..9001)
em.photo = File.open(IMAGES.sample) unless NO_PICS
em.associated_equipment_models = EquipmentModel.all.sample(6)
em.ordering = EquipmentModel.count
end
end
end
3 changes: 2 additions & 1 deletion lib/seed/reservation_generator_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ def gen_res(random = false)
notes: FFaker::HipsterIpsum.paragraph(2),
start_date: Time.zone.today)
max_checkout_len = r.equipment_model.maximum_checkout_length
last = [max_checkout_len - 1, 1].max
duration = max_checkout_len -
rand_val(first: 1, last: max_checkout_len - 1,
rand_val(first: 1, last: last,
default: 1, random: random)
r.due_date = r.start_date + duration.days
r
Expand Down
7 changes: 3 additions & 4 deletions lib/tasks/style_checker.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ task :check_style do
puts diff_output
puts "\nRunning rubocop..."
puts check_ruby
puts "\nRunning eslint..."
puts check_js
# puts "\nRunning eslint..."
# puts check_js
exit evaluate
end

Expand Down Expand Up @@ -42,8 +42,7 @@ def evaluate
end

def passed?
RUBY_PASS.any? { |m| check_ruby.include? m } &&
JS_PASS.any? { |m| check_js.include? m }
RUBY_PASS.any? { |m| check_ruby.include? m }
end

def rubocop(files)
Expand Down
Loading

0 comments on commit 9ff6aa1

Please sign in to comment.