Skip to content

Commit a14365a

Browse files
committed
very basic first implementation of dendro
1 parent 32594ce commit a14365a

38 files changed

+1033
-4
lines changed

app/controllers/dendros_controller.rb

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
class DendrosController < ApplicationController
2+
include Tabulatable
3+
include Pagy::Backend
4+
5+
load_and_authorize_resource
6+
7+
before_action :set_dendro, only: %i[ show edit update destroy ]
8+
before_action :set_site, only: [ :new ]
9+
10+
# GET /dendros or /dendros.json
11+
def index
12+
@dendros = Dendro.includes(
13+
{sample: [
14+
:material,
15+
:taxon,
16+
:context
17+
]},
18+
:references
19+
)
20+
21+
# filter
22+
unless dendro_params.blank?
23+
@dendros = @dendros.where(dendro_params)
24+
end
25+
26+
# if params[:sample_attributes][:context_attributes][:site_id].present?
27+
# @dendros = @dendros.joins(sample: { context: :site }).where(sample:{context:{sites:{id: params[:sample_attributes][:context_attributes][:site_id]}}})
28+
# end
29+
30+
# order
31+
if params.has_key?(:dendros_order_by)
32+
order = { params[:dendros_order_by] => params.fetch(:dendros_order, "asc") }
33+
@dendros = @dendros.reorder(order)
34+
end
35+
36+
respond_to do |format|
37+
format.html {
38+
@pagy, @dendros = pagy(@dendros)
39+
}
40+
format.json
41+
format.csv {
42+
@dendros = @dendros.select(index_csv_template)
43+
render csv: @dendros
44+
}
45+
end
46+
end
47+
48+
# GET /dendros/1 or /dendros/1.json
49+
def show
50+
end
51+
52+
# GET /dendros/new
53+
def new
54+
@dendro = Dendro.new
55+
@dendro.build_sample.build_context(site: @site) # Prebuild the nested structure
56+
# @context = @site.contexts.build
57+
# @sample = @context.samples.build
58+
# @dendro = @sample.dendros.build
59+
end
60+
61+
# GET /dendros/1/edit
62+
def edit
63+
end
64+
65+
# POST /dendros or /dendros.json
66+
def create
67+
@dendro = Dendro.new(dendro_params)
68+
69+
respond_to do |format|
70+
if @dendro.save
71+
format.html { redirect_to @dendro, notice: "Dendro was successfully created." }
72+
format.json { render :show, status: :created, location: @dendro }
73+
else
74+
format.html { render :new, status: :unprocessable_entity }
75+
format.json { render json: @dendro.errors, status: :unprocessable_entity }
76+
end
77+
end
78+
end
79+
80+
# PATCH/PUT /dendros/1 or /dendros/1.json
81+
def update
82+
respond_to do |format|
83+
if @dendro.update(dendro_params)
84+
format.html { redirect_to @dendro, notice: "Dendro was successfully updated." }
85+
format.json { render :show, status: :ok, location: @dendro }
86+
else
87+
format.html { render :edit, status: :unprocessable_entity }
88+
format.json { render json: @dendro.errors, status: :unprocessable_entity }
89+
end
90+
end
91+
end
92+
93+
# DELETE /dendros/1 or /dendros/1.json
94+
def destroy
95+
@dendro.destroy
96+
97+
respond_to do |format|
98+
format.html { redirect_to dendros_path, status: :see_other, notice: "Dendro was successfully destroyed." }
99+
format.json { head :no_content }
100+
end
101+
end
102+
103+
private
104+
# Use callbacks to share common setup or constraints between actions.
105+
def set_dendro
106+
@dendro = Dendro.find(params[:id])
107+
end
108+
109+
def set_site
110+
@site = Site.find(params[:site])
111+
end
112+
113+
# Never trust parameters from the scary internet, only allow the white list through.
114+
def dendro_params
115+
params.fetch(:dendro, {}).permit(
116+
:sample_id,
117+
:series_code,
118+
:name,
119+
:description,
120+
:start_year,
121+
:end_year,
122+
:is_anchored,
123+
:offset,
124+
:measurements,
125+
{sample_attributes: [
126+
:id,
127+
:material_id,
128+
:taxon_id,
129+
:context_id,
130+
{context_attributes: [
131+
:id,
132+
:name,
133+
:approx_start_time,
134+
:approx_end_time,
135+
:site_id
136+
]},
137+
:position_description,
138+
:position_x,
139+
:position_y,
140+
:position_z,
141+
:position_crs
142+
]},
143+
sample: [
144+
:context_id,
145+
contexts: [
146+
:site_id
147+
]
148+
]
149+
)
150+
end
151+
end

app/controllers/sites_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def show
6060
order = { params[:c14s_order_by] => params.fetch(:c14s_order, "asc") }
6161
@c14s = @c14s.reorder(order)
6262
end
63+
64+
@dendros = @site.dendros.includes([:references, sample: [ :material, :taxon, :context ]])
65+
if params.has_key?(:dendros_order_by)
66+
order = { params[:dendros_order_by] => params.fetch(:dendros_order, "asc") }
67+
@dendros = @dendros.reorder(order)
68+
end
6369

6470
@typos = @site.typos.includes([:references])
6571
if params.has_key?(:typos_order_by)
@@ -71,6 +77,7 @@ def show
7177
format.html {
7278
@pagy_c14s, @c14s = pagy(@c14s, page_param: :c14s_page)
7379
@pagy_typos, @typos = pagy(@typos, page_param: :typos_page)
80+
@pagy_dendros, @dendros = pagy(@dendros, page_param: :dendros_page)
7481
}
7582
format.json
7683
end

app/helpers/dendros_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module DendrosHelper
2+
end

app/models/context.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Context < ApplicationRecord
2626
has_many :samples
2727
has_many :c14s, through: :samples
2828
has_many :typos, through: :samples
29+
has_many :dendros, through: :samples
2930
has_paper_trail
3031

3132
acts_as_copy_target # enable CSV exports

app/models/dendro.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# == Schema Information
2+
#
3+
# Table name: dendros
4+
#
5+
# id :bigint not null, primary key
6+
# description :text
7+
# end_year :integer
8+
# is_anchored :boolean default(FALSE)
9+
# measurements :jsonb not null
10+
# name :string not null
11+
# offset :integer
12+
# series_code :string not null
13+
# start_year :integer
14+
# created_at :datetime not null
15+
# updated_at :datetime not null
16+
# sample_id :bigint not null
17+
#
18+
# Indexes
19+
#
20+
# index_dendros_on_measurements (measurements) USING gin
21+
# index_dendros_on_sample_id (sample_id)
22+
# index_dendros_on_series_code (series_code) UNIQUE
23+
#
24+
# Foreign Keys
25+
#
26+
# fk_rails_... (sample_id => samples.id)
27+
#
28+
class Dendro < ApplicationRecord
29+
belongs_to :sample
30+
accepts_nested_attributes_for :sample, reject_if: :all_blank
31+
32+
has_many :citations, as: :citing
33+
has_many :references, :through => :citations
34+
35+
delegate :context, to: :sample
36+
delegate :site, to: :sample
37+
38+
validates_associated :sample
39+
40+
include Versioned
41+
42+
def self.label
43+
"dendrochronological series"
44+
end
45+
46+
def self.icon
47+
"icons/dendro.svg"
48+
end
49+
end

app/models/sample.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Sample < ApplicationRecord
4141

4242
has_many :c14s
4343
has_many :typos
44+
has_many :dendros
4445

4546
include Versioned
4647

app/models/site.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Site < ApplicationRecord
2323
has_many :samples, through: :contexts
2424
has_many :c14s, through: :contexts
2525
has_many :typos, through: :contexts
26+
has_many :dendros, through: :contexts
2627

2728
has_and_belongs_to_many :site_types, optional: true
2829

@@ -117,6 +118,10 @@ def n_c14s
117118
def n_typos
118119
typos.count
119120
end
121+
122+
def n_dendros
123+
typos.count
124+
end
120125

121126
def recursive_references
122127
c14_references = c14s.map(&:references).reduce(:+)

app/views/contexts/_fields.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<div class="row">
44
<div class="col"><%= f.number_field :approx_start_time, label: "Dated from", prepend: "c.", append: "cal BP" %></div>
55
<div class="col"><%= f.number_field :approx_end_time, label: "Dated to", prepend: "c.", append: "cal BP" %></div>
6-
</div>
6+
</div>

app/views/dendros/_dendro.html.erb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<div id="<%= dom_id dendro %>">
2+
<p>
3+
<strong>Sample:</strong>
4+
<%= dendro.sample_id %>
5+
</p>
6+
7+
<p>
8+
<strong>Series code:</strong>
9+
<%= dendro.series_code %>
10+
</p>
11+
12+
<p>
13+
<strong>Name:</strong>
14+
<%= dendro.name %>
15+
</p>
16+
17+
<p>
18+
<strong>Description:</strong>
19+
<%= dendro.description %>
20+
</p>
21+
22+
<p>
23+
<strong>Start year:</strong>
24+
<%= dendro.start_year %>
25+
</p>
26+
27+
<p>
28+
<strong>End year:</strong>
29+
<%= dendro.end_year %>
30+
</p>
31+
32+
<p>
33+
<strong>Is anchored:</strong>
34+
<%= dendro.is_anchored %>
35+
</p>
36+
37+
<p>
38+
<strong>Offset:</strong>
39+
<%= dendro.offset %>
40+
</p>
41+
42+
<p>
43+
<strong>Measurements:</strong>
44+
<%= dendro.measurements %>
45+
</p>
46+
47+
</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! dendro, :id, :sample_id, :series_code, :name, :description, :start_year, :end_year, :is_anchored, :offset, :measurements, :created_at, :updated_at
2+
json.url dendro_url(dendro, format: :json)

0 commit comments

Comments
 (0)