forked from fixin.me/fixin.me
Compare commits
1 Commits
fix/measur
...
fix-sqlite
| Author | SHA1 | Date | |
|---|---|---|---|
| 8be9b3eb2f |
@@ -1,13 +1,7 @@
|
|||||||
class MeasurementsController < ApplicationController
|
class MeasurementsController < ApplicationController
|
||||||
before_action except: :index do
|
|
||||||
raise AccessForbidden unless current_user.at_least(:active)
|
|
||||||
end
|
|
||||||
|
|
||||||
def index
|
def index
|
||||||
readouts = current_user.readouts.includes(:quantity, :unit).order(created_at: :desc)
|
@measurements = []
|
||||||
@measurements = readouts.group_by(&:created_at).map do |created_at, grouped|
|
#@measurements = current_user.units.ordered.includes(:base, :subunits)
|
||||||
Measurement.new(created_at: created_at, readouts: grouped)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@@ -15,33 +9,8 @@ class MeasurementsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
timestamp = Time.current
|
|
||||||
@readouts = readout_params.map do |rp|
|
|
||||||
r = current_user.readouts.new(rp)
|
|
||||||
r.created_at = timestamp
|
|
||||||
r
|
|
||||||
end
|
|
||||||
|
|
||||||
if @readouts.all?(&:valid?)
|
|
||||||
Readout.transaction { @readouts.each(&:save!) }
|
|
||||||
@measurement = Measurement.new(readouts: @readouts, created_at: timestamp)
|
|
||||||
flash.now[:notice] = t('.success')
|
|
||||||
else
|
|
||||||
render :new, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@measurement = Measurement.new(id: params[:id].to_i,
|
|
||||||
created_at: Time.at(params[:id].to_i))
|
|
||||||
current_user.readouts.where(created_at: @measurement.created_at).delete_all
|
|
||||||
@measurements_empty = current_user.readouts.empty?
|
|
||||||
flash.now[:notice] = t('.success')
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def readout_params
|
|
||||||
params.require(:readouts).map { |r| r.permit(:quantity_id, :value, :unit_id) }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,17 +1,3 @@
|
|||||||
class Measurement
|
class Measurement
|
||||||
include ActiveModel::Model
|
include ActiveModel::Model
|
||||||
|
|
||||||
attr_accessor :readouts, :created_at
|
|
||||||
|
|
||||||
def id
|
|
||||||
created_at.to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_param
|
|
||||||
id.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def persisted?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ class Quantity < ApplicationRecord
|
|||||||
errors.add(:parent, :descendant_reference) if ancestor_of?(parent)
|
errors.add(:parent, :descendant_reference) if ancestor_of?(parent)
|
||||||
end
|
end
|
||||||
validates :name, presence: true, uniqueness: {scope: [:user_id, :parent_id]},
|
validates :name, presence: true, uniqueness: {scope: [:user_id, :parent_id]},
|
||||||
length: {maximum: type_for_attribute(:name).limit || Float::INFINITY}
|
length: {maximum: type_for_attribute(:name).limit}
|
||||||
validates :description, length: {maximum: type_for_attribute(:description).limit || Float::INFINITY}
|
validates :description, length: {maximum: type_for_attribute(:description).limit}
|
||||||
|
|
||||||
# Update :depths of progenies after parent change
|
# Update :depths of progenies after parent change
|
||||||
before_save if: :parent_changed? do
|
before_save if: :parent_changed? do
|
||||||
@@ -61,26 +61,18 @@ class Quantity < ApplicationRecord
|
|||||||
|
|
||||||
# Return: ordered [sub]hierarchy
|
# Return: ordered [sub]hierarchy
|
||||||
scope :ordered, ->(root: nil, include_root: true) {
|
scope :ordered, ->(root: nil, include_root: true) {
|
||||||
if connection.adapter_name =~ /mysql/i
|
numbered = Arel::Table.new('numbered')
|
||||||
numbered = Arel::Table.new('numbered')
|
|
||||||
self.model.with(numbered: numbered(:parent_id, :name)).with_recursive(arel_table.name => [
|
self.model.with(numbered: numbered(:parent_id, :name)).with_recursive(arel_table.name => [
|
||||||
numbered.project(
|
numbered.project(
|
||||||
numbered[Arel.star],
|
numbered[Arel.star],
|
||||||
numbered.cast(numbered[:child_number], 'BINARY').as('path')
|
numbered.cast(numbered[:child_number], connection.adapter_name == 'Mysql2' ? 'BINARY' : 'BLOB').as('path')
|
||||||
).where(numbered[root && include_root ? :id : :parent_id].eq(root)),
|
).where(numbered[root && include_root ? :id : :parent_id].eq(root)),
|
||||||
numbered.project(
|
numbered.project(
|
||||||
numbered[Arel.star],
|
numbered[Arel.star],
|
||||||
arel_table[:path].concat(numbered[:child_number])
|
arel_table[:path].concat(numbered[:child_number])
|
||||||
).join(arel_table).on(numbered[:parent_id].eq(arel_table[:id]))
|
).join(arel_table).on(numbered[:parent_id].eq(arel_table[:id]))
|
||||||
]).order(arel_table[:path])
|
]).order(arel_table[:path])
|
||||||
elsif root.nil?
|
|
||||||
# SQLite: pathname column already stores the full hierarchical path
|
|
||||||
order(:pathname)
|
|
||||||
else
|
|
||||||
root_pathname = unscoped.where(id: root).pick(:pathname)
|
|
||||||
scope = order(:pathname).where("pathname LIKE ?", "#{root_pathname}#{PATHNAME_DELIMITER}%")
|
|
||||||
include_root ? scope.or(where(id: root)) : scope
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: extract named functions to custom Arel extension
|
# TODO: extract named functions to custom Arel extension
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
<%= tag.tr id: dom_id(measurement) do %>
|
|
||||||
<td><%= l measurement.created_at, format: :short %></td>
|
|
||||||
<td>
|
|
||||||
<% measurement.readouts.each do |readout| %>
|
|
||||||
<span><%= readout.quantity.name %>: <%= readout.value %> <%= readout.unit %></span>
|
|
||||||
<% end %>
|
|
||||||
</td>
|
|
||||||
<% if current_user.at_least(:active) %>
|
|
||||||
<td class="actions">
|
|
||||||
<%= image_button_to t('.destroy'), 'delete-outline', measurement_path(measurement),
|
|
||||||
method: :delete %>
|
|
||||||
</td>
|
|
||||||
<% end %>
|
|
||||||
<% end %>
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<%= turbo_stream.update :flashes %>
|
|
||||||
<%= turbo_stream.remove :measurement_form %>
|
|
||||||
<%= turbo_stream.remove :no_items %>
|
|
||||||
<%= turbo_stream.enable :new_measurement_link %>
|
|
||||||
<%= turbo_stream.prepend :measurements, @measurement %>
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
<%= turbo_stream.update :flashes %>
|
|
||||||
<%= turbo_stream.remove @measurement %>
|
|
||||||
<%= turbo_stream.append(:measurements, render_no_items) if @measurements_empty %>
|
|
||||||
@@ -88,12 +88,6 @@ en:
|
|||||||
select_quantity: select the measured quantities...
|
select_quantity: select the measured quantities...
|
||||||
index:
|
index:
|
||||||
new_measurement: Add measurement
|
new_measurement: Add measurement
|
||||||
create:
|
|
||||||
success: Measurement saved.
|
|
||||||
destroy:
|
|
||||||
success: Measurement deleted.
|
|
||||||
measurement:
|
|
||||||
destroy: Delete
|
|
||||||
readouts:
|
readouts:
|
||||||
form:
|
form:
|
||||||
select_unit: ...
|
select_unit: ...
|
||||||
|
|||||||
Reference in New Issue
Block a user