1
0

Finished Sources implementation

This commit is contained in:
cryptogopher 2019-09-23 20:13:09 +02:00
parent 35f8ec4b2c
commit 593e11ff53
7 changed files with 64 additions and 3 deletions

View File

@ -7,7 +7,7 @@ class IngredientsController < ApplicationController
def index
@ingredient = @project.ingredients.new
# passing attr for after_initialize
# passing attr for Nutrient after_initialize
@ingredient.nutrients.new(ingredient: @ingredient)
@ingredients = @project.ingredients.includes(:ref_unit)
@ingredients << @ingredient

View File

@ -39,7 +39,7 @@ class SourcesController < ApplicationController
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
def find_source
@unit = Source.find(params[:id])
@source = Source.find(params[:id])
@project = @source.project
rescue ActiveRecord::RecordNotFound
render_404

View File

@ -1,2 +1,5 @@
class Source < ActiveRecord::Base
belongs_to :project, required: false
validates :name, presence: true, uniqueness: {scope: :project_id}
end

View File

@ -0,0 +1,6 @@
<%= error_messages_for @source %>
<div class="box tabular">
<p><%= f.text_field :name, required: true, size: 20 %></p>
<p><%= f.text_area :description, rows: 3, cols: 60 %></p>
</div>

View File

@ -1 +1,48 @@
<h2>SourcesController#index</h2>
<% content_for :sidebar do %>
<%= render :partial => 'body_trackers/sidebar' %>
<% end %>
<div class="contextual">
<% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".heading_new_source"), '#', :class => 'icon icon-add',
:onclick => 'showAndScrollTo("add-source", "source_name"); return false;' %>
<% end %>
</div>
<div id="add-source" <%= 'style=display:none;' if @source.errors.empty? %>>
<h2><%= t ".heading_new_source" %></h2>
<%= labelled_form_for @source,
:url => project_sources_path(@project),
:html => {:id => 'source-form'} do |f| %>
<%= render :partial => 'sources/form', :locals => { :f => f } %>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-source").hide()' %>
<% end %>
<hr>
</div>
<h2><%= t ".heading" %></h2>
<% if @sources.many? %>
<table class="list">
<thead>
<tr>
<th><%= l(:field_name) %></th>
<th><%= l(:field_description) %></th>
<th style="width:15%"><%= l(:field_action) %></th>
</tr>
</thead>
<tbody>
<% @sources.each do |s| %>
<% next if s.new_record? %>
<tr id="source-<%= s.id %>" class="source">
<td class="name"><%= s.name %></td>
<td class="description"><%= s.description %></td>
<td><%= delete_link source_path(s), data: {} %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>

View File

@ -54,6 +54,10 @@ en:
groups:
other: 'other'
meat: 'meat'
sources:
index:
heading: 'Data sources'
heading_new_source: 'New source'
quantities:
index:
heading: 'Quantities'

View File

@ -3,6 +3,7 @@ module BodyTracking
Project.class_eval do
has_many :ingredients, -> { order "name" }, dependent: :destroy
has_many :sources, dependent: :destroy
has_many :quantities, -> { order "lft" }, dependent: :destroy
has_many :units, dependent: :destroy
end