From b0789e6217bbc6b86fe5e2cd9d866250e332bef5 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 10 Aug 2019 23:31:20 +0200 Subject: [PATCH] Added new Unit form --- app/controllers/body_trackers_controller.rb | 5 +--- app/controllers/units_controller.rb | 21 +++++++++++++++++ app/views/body_trackers/_sidebar.html.erb | 2 +- app/views/body_trackers/units.html.erb | 5 ---- app/views/units/_form.html.erb | 7 ++++++ app/views/units/index.html.erb | 26 +++++++++++++++++++++ config/locales/en.yml | 5 +++- config/routes.rb | 7 ++---- db/migrate/001_create_units.rb | 1 + init.rb | 3 ++- 10 files changed, 65 insertions(+), 17 deletions(-) delete mode 100644 app/views/body_trackers/units.html.erb create mode 100644 app/views/units/_form.html.erb create mode 100644 app/views/units/index.html.erb diff --git a/app/controllers/body_trackers_controller.rb b/app/controllers/body_trackers_controller.rb index a081878..77f94be 100644 --- a/app/controllers/body_trackers_controller.rb +++ b/app/controllers/body_trackers_controller.rb @@ -1,13 +1,10 @@ class BodyTrackersController < ApplicationController - before_action :find_project, only: [:index, :units] + before_action :find_project, only: [:index] before_action :authorize def index end - def units - end - private # :find_* methods are called before :authorize, diff --git a/app/controllers/units_controller.rb b/app/controllers/units_controller.rb index 7cff925..36ed09a 100644 --- a/app/controllers/units_controller.rb +++ b/app/controllers/units_controller.rb @@ -1,7 +1,28 @@ class UnitsController < ApplicationController + before_action :find_project, only: [:new, :index, :create] + before_action :authorize + + def new + @unit = Unit.new + end + + def index + @unit = Unit.new + end + def create end def destroy end + + private + + # :find_* methods are called before :authorize, + # @project is required for :authorize to succeed + def find_project + @project = Project.find(params[:project_id]) + rescue ActiveRecord::RecordNotFound + render_404 + end end diff --git a/app/views/body_trackers/_sidebar.html.erb b/app/views/body_trackers/_sidebar.html.erb index ec1abcd..1a3c95f 100644 --- a/app/views/body_trackers/_sidebar.html.erb +++ b/app/views/body_trackers/_sidebar.html.erb @@ -5,5 +5,5 @@

<%= t ".heading_common" %>

diff --git a/app/views/body_trackers/units.html.erb b/app/views/body_trackers/units.html.erb deleted file mode 100644 index 12b2ca1..0000000 --- a/app/views/body_trackers/units.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

<%= t ".heading" %>

- -<% content_for :sidebar do %> - <%= render :partial => 'body_trackers/sidebar' %> -<% end %> diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb new file mode 100644 index 0000000..5c4e01e --- /dev/null +++ b/app/views/units/_form.html.erb @@ -0,0 +1,7 @@ +<%= error_messages_for @unit %> + +
+

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

+

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

+

<%#= f.select :summary, :cols => 60, :rows => 2 %>

+
diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb new file mode 100644 index 0000000..44a0cfe --- /dev/null +++ b/app/views/units/index.html.erb @@ -0,0 +1,26 @@ +<% content_for :sidebar do %> + <%= render :partial => 'body_trackers/sidebar' %> +<% end %> + +
+ <% if @project && User.current.allowed_to?(:manage_units, @project) %> + <%= link_to t(".heading_new_unit"), new_project_unit_path(@project), + :class => 'icon icon-add', + :onclick => 'showAndScrollTo("add-unit", "unit_name"); return false;' %> + <% end %> +
+ + + +

<%= t ".heading" %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index ae4cf8c..48bcbe2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,6 +1,7 @@ # English strings go here for Rails i18n en: body_trackers_menu_caption: 'Body trackers' + field_shortname: 'Short name' body_trackers: index: heading: 'Summary' @@ -9,5 +10,7 @@ en: heading_common: 'Common' link_summary: 'Summary' link_units: 'Units' - units: + units: + index: heading: 'Units' + heading_new_unit: 'New unit' diff --git a/config/routes.rb b/config/routes.rb index 9de821b..8d263bb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,10 +3,7 @@ resources :projects do shallow do - resources :body_trackers, :controller => 'body_trackers', :only => [:index] do - collection do - get 'units' - end - end + resources :body_trackers, :only => [:index] + resources :units, :only => [:new, :index, :create, :destroy] end end diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index 48d3f65..e6e0333 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -1,6 +1,7 @@ class CreateUnits < ActiveRecord::Migration def change create_table :units do |t| + t.references :project_id t.string :name t.string :shortname t.integer :type diff --git a/init.rb b/init.rb index 61967a3..31053a1 100644 --- a/init.rb +++ b/init.rb @@ -7,7 +7,8 @@ Redmine::Plugin.register :body_tracking do author_url 'https://github.com/cryptogopher' project_module :body_tracking do - permission :view_body_trackers, {:body_trackers => [:index, :units]}, read: true + permission :view_body_trackers, {:body_trackers => [:index], :units => [:index]}, read: true + permission :manage_units, {:units => [:new, :create, :destroy]}, require: :loggedin end menu :project_menu, :body_trackers, {:controller => 'body_trackers', :action => 'index'},