From 8e7385cdcb9d5a5b5e1985cbdc9f02e1cfc97f34 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Wed, 21 Aug 2019 22:54:13 +0200 Subject: [PATCH] Removed Unit.group, added UnitsController#import --- app/controllers/units_controller.rb | 5 ++++- app/helpers/units_helper.rb | 4 ---- app/models/unit.rb | 24 ------------------------ app/views/units/_form.html.erb | 3 +-- config/locales/en.yml | 16 ---------------- config/routes.rb | 4 +++- db/migrate/001_create_units.rb | 12 +++++------- 7 files changed, 13 insertions(+), 55 deletions(-) diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb index 36ed09a..ac78ff7 100644 --- a/app/controllers/units_controller.rb +++ b/app/controllers/units_controller.rb @@ -1,5 +1,5 @@ class UnitsController < ApplicationController - before_action :find_project, only: [:new, :index, :create] + before_action :find_project, only: [:new, :index, :create, :import] before_action :authorize def new @@ -16,6 +16,9 @@ class UnitsController < ApplicationController def destroy end + def import + end + private # :find_* methods are called before :authorize, diff --git a/app/helpers/units_helper.rb b/app/helpers/units_helper.rb index b1d9fe1..d8b2f3f 100644 --- a/app/helpers/units_helper.rb +++ b/app/helpers/units_helper.rb @@ -1,6 +1,2 @@ module UnitsHelper - def group_options - translations = t('.groups') - Unit.groups.map { |k,v| [translations[k.to_sym], k] } - end end diff --git a/app/models/unit.rb b/app/models/unit.rb index 2a20bdf..8923c66 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -1,30 +1,6 @@ class Unit < ActiveRecord::Base belongs_to :project - # https://en.wikipedia.org/wiki/International_System_of_Units - enum group: { - number: 0, - share: 1, - - length: 10, - mass: 11, - time: 12, - temperature: 13, - - volume: 20, - density: 21, - ndensity: 22, - - frequency: 30, - velocity: 31, - flow: 32, - - energy: 30, - - pressure: 40 - } - validates :project, associated: true validates :name, :shortname, presence: true - validates :group, inclusion: groups.keys end diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb index 87404dc..a8838f4 100644 --- a/app/views/units/_form.html.erb +++ b/app/views/units/_form.html.erb @@ -6,8 +6,7 @@

<%= f.text_field :shortname, required: true, size: 10 %>

-

<%= f.select :group, group_options, required: true %>

+

<%= f.text_field :name, required: true, size: 40 %>

-

<%= f.text_field :name, required: true, size: 40 %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index e6ddaee..48bcbe2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,19 +14,3 @@ en: index: heading: 'Units' heading_new_unit: 'New unit' - form: - groups: - number: 'count' - share: 'share' - length: 'length' - mass: 'mass' - time: 'time' - temperature: 'temperature' - volume: 'volume' - density: 'density' - ndensity: 'number density' - frequency: 'frequency' - velocity: 'velocity' - flow: 'flow rate' - energy: 'energy' - pressure: 'pressure' diff --git a/config/routes.rb b/config/routes.rb index 8d263bb..61e26b2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ resources :projects do shallow do resources :body_trackers, :only => [:index] - resources :units, :only => [:new, :index, :create, :destroy] + resources :units, :only => [:new, :index, :create, :destroy] do + post 'import', on: :collection + end end end diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index 6a73d19..ba81eb1 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -4,19 +4,17 @@ class CreateUnits < ActiveRecord::Migration t.references :project t.string :name t.string :shortname - t.integer :group end reversible do |dir| dir.up do - Unit.create project: nil, shortname: "", name: "count", group: :number - Unit.create project: nil, shortname: "%", name: "percent", group: :share - Unit.create project: nil, shortname: "g", name: "gram", group: :mass - Unit.create project: nil, shortname: "kg", name: "kilogram", group: :mass + Unit.create project: nil, shortname: "", name: "count" + Unit.create project: nil, shortname: "%", name: "percent" + Unit.create project: nil, shortname: "g", name: "gram" + Unit.create project: nil, shortname: "kg", name: "kilogram" end - dir.down do - Unit.delete_all + Unit.where(project: nil).delete_all end end end