uchi 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45c91f06accc01dce5031d673f7c0a78c71174fadf6d079dc288b68d815b4a9c
4
- data.tar.gz: 76542812dc454f122bb54ed3a86b0857860ff5ff633cf3a9a3f120ea28bf782c
3
+ metadata.gz: 89d20ec9aa3f6dad006bcf717d489b69fd889d6f8ad37ac484b9a146b52ef843
4
+ data.tar.gz: f6e3c52325eff6be90d814a2158083cf3f2f521f689539c9a23bf30d20246ab4
5
5
  SHA512:
6
- metadata.gz: ec71b43f8744a50005922ba80cd6224ce88cd8c90e4bbf70eb33d9017e327f184bb032f9a1609b3e1c48430b2345cc96a42644eee4cbb63a30577ee530995724
7
- data.tar.gz: c8e26d886f9153251caf74d123883d529c3247b8252048be18706e65174688223db6f03c0ca7f027f95bab9df278446c951664c55a57727cab212f549b96c9df
6
+ metadata.gz: cf626c8ce1d6a0d5e22ca6df5f190c5def32b4e02d81510429b281cfa54ab79f95fe57745cdce76c4fb54487ce66547521778be78b319cedbcb283de17b5c121
7
+ data.tar.gz: e913266e035d095b1e5c801ba6da2867d9dad9dec026bc8e1cc379d49b9bc6693517416b76ac3393798f09468a9090688ecfb7cb2ab7ffe59eb2fe2a55cffc97
@@ -1,3 +1,5 @@
1
- <%= render(Flowbite::Link.new(
2
- href: associated_repository.routes.path_for(:show, id: associated_record.id)
3
- ).with_content(associated_record.to_s)) %>
1
+ <% if associated_record %>
2
+ <%= render(Flowbite::Link.new(
3
+ href: associated_repository.routes.path_for(:show, id: associated_record.id)
4
+ ).with_content(associated_record.to_s)) %>
5
+ <% end %>
@@ -25,11 +25,18 @@ module Uchi
25
25
 
26
26
  def collection_for_select
27
27
  repository = associated_repository
28
- collection.map do |item|
28
+ items = []
29
+ items << ["", nil] if optional?
30
+ items + collection.map do |item|
29
31
  [repository.title(item), item.id]
30
32
  end
31
33
  end
32
34
 
35
+ # Returns true if the association is optional.
36
+ def optional?
37
+ reflection.options[:optional] == true
38
+ end
39
+
33
40
  def options
34
41
  options = {
35
42
  attribute: attribute_name,
@@ -5,9 +5,9 @@
5
5
  <% end %>
6
6
  </div>
7
7
 
8
- <div class="items-center justify-between space-x-6 space-y-3 md:flex md:px-0">
8
+ <div class="items-start justify-between space-x-6 md:flex md:px-0">
9
9
  <div>
10
- <h1 class="mb-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-white group"><%= title %></h1>
10
+ <h1 class="text-3xl font-semibold tracking-tight text-gray-900 dark:text-white group"><%= title %></h1>
11
11
  <% if description.present? %>
12
12
  <div class="text-lg text-gray-500 lg:mb-0 dark:text-gray-400 lg:max-w-2xl"><%= description %></div>
13
13
  <% end %>
@@ -13,10 +13,10 @@
13
13
  <div class="md:flex">
14
14
  <nav class="w-64 h-full px-3 py-4 overflow-y-auto shrink-0 bg-gray-50 dark:bg-gray-800" aria-label="Main">
15
15
  <ul class="space-y-2 font-medium">
16
- <% Uchi::Repository.all.map { |repository| repository.new }.sort_by { |repository| repository.translate.plural_name.downcase }.each do |repository| %>
16
+ <% Uchi::Repository.all.map { |repository| repository.new }.sort_by { |repository| repository.translate.navigation_label.downcase }.each do |repository| %>
17
17
  <li>
18
18
  <%= link_to(repository.routes.path_for(:index), :class => "flex items-center p-2 text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700 group") do %>
19
- <span class="ms-3"><%= repository.translate.plural_name.titlecase %></span>
19
+ <span class="ms-3"><%= repository.translate.navigation_label %></span>
20
20
  <% end %>
21
21
  </li>
22
22
  <% end %>
@@ -1,12 +1,12 @@
1
- <%- content_for(:page_title) { @repository.translate.title(:edit, record: @record) } %>
1
+ <%- content_for(:page_title) { @repository.translate.title_for_edit(@record) } %>
2
2
 
3
3
  <%= render(
4
4
  Uchi::Ui::PageHeader.new(
5
5
  description: @repository.translate.description(:edit),
6
- title: @repository.translate.title(:edit, record: @record),
6
+ title: @repository.translate.title_for_edit(@record),
7
7
  )) do |header| %>
8
8
  <% header.with_breadcrumb(items: [
9
- {href: @repository.routes.root_path, label: Rails.application.name.titlecase },
9
+ {href: @repository.routes.root_path, label: @repository.translate.breadcrumb_label_for_root },
10
10
  {href: @repository.routes.path_for(:index), label: @repository.translate.breadcrumb_label(:index) },
11
11
  {href: @repository.routes.path_for(:show, id: @record.id), label: @repository.translate.breadcrumb_label(:show, record: @record) },
12
12
  {href: @repository.routes.path_for(:edit, id: @record.id), label: @repository.translate.breadcrumb_label(:edit, record: @record) }
@@ -1,15 +1,15 @@
1
- <%- content_for(:page_title) { @repository.translate.title(:index) } %>
1
+ <%- content_for(:page_title) { @repository.translate.title_for_index } %>
2
2
 
3
3
  <%= render(
4
4
  Uchi::Ui::PageHeader.new(
5
5
  description: @repository.translate.description(:index),
6
- title: @repository.translate.title(:index),
6
+ title: @repository.translate.title_for_index,
7
7
  )) do |header| %>
8
8
  <% header.with_action do %>
9
9
  <%= link_to(@repository.translate.link_to_new, @repository.routes.path_for(:new), class: Flowbite::Button.classes) %>
10
10
  <% end %>
11
11
  <% header.with_breadcrumb(items: [
12
- {href: @repository.routes.root_path, label: Rails.application.name.titlecase },
12
+ {href: @repository.routes.root_path, label: @repository.translate.breadcrumb_label_for_root },
13
13
  {href: @repository.routes.path_for(:index), label: @repository.translate.breadcrumb_label(:index) }
14
14
  ]) %>
15
15
  <% end %>
@@ -1,12 +1,12 @@
1
- <%- content_for(:page_title) { @repository.translate.title(:new) } %>
1
+ <%- content_for(:page_title) { @repository.translate.title_for_new } %>
2
2
 
3
3
  <%= render(
4
4
  Uchi::Ui::PageHeader.new(
5
5
  description: @repository.translate.description(:new),
6
- title: @repository.translate.title(:new),
6
+ title: @repository.translate.title_for_new,
7
7
  )) do |header| %>
8
8
  <% header.with_breadcrumb(items: [
9
- {href: @repository.routes.root_path, label: Rails.application.name.titlecase },
9
+ {href: @repository.routes.root_path, label: @repository.translate.breadcrumb_label_for_root },
10
10
  {href: @repository.routes.path_for(:index), label: @repository.translate.breadcrumb_label(:index) },
11
11
  {href: @repository.routes.path_for(:new), label: @repository.translate.breadcrumb_label(:new) }
12
12
  ]) %>
@@ -1,9 +1,9 @@
1
- <%- content_for(:page_title) { @repository.translate.title(:show, record: @record) } %>
1
+ <%- content_for(:page_title) { @repository.translate.title_for_show(@record) } %>
2
2
 
3
3
  <%= render(
4
4
  Uchi::Ui::PageHeader.new(
5
5
  description: @repository.translate.description(:show, record: @record),
6
- title: @repository.translate.title(:show, record: @record),
6
+ title: @repository.translate.title_for_show(@record),
7
7
  )
8
8
  ) do |header| %>
9
9
  <% header.with_action do %>
@@ -23,7 +23,7 @@
23
23
  <% end %>
24
24
 
25
25
  <% header.with_breadcrumb(items: [
26
- {href: @repository.routes.root_path, label: Rails.application.name.titlecase },
26
+ {href: @repository.routes.root_path, label: @repository.translate.breadcrumb_label_for_root },
27
27
  {href: @repository.routes.path_for(:index), label: @repository.translate.breadcrumb_label(:index) },
28
28
  {href: @repository.routes.path_for(:show, id: @record.id), label: @repository.translate.breadcrumb_label(:show, record: @record) }
29
29
  ]) %>
@@ -39,11 +39,23 @@ module Uchi
39
39
  translate(i18n_scope("breadcrumb.index.label"), default: nil),
40
40
  translate(i18n_scope("index.title"), default: nil),
41
41
  plural_name,
42
- translate("common.index"),
42
+ translate("common.index", default: nil),
43
43
  "Index"
44
44
  )
45
45
  end
46
46
 
47
+ # Returns the label for the root breadcrumb item. Defaults to the
48
+ # application name.
49
+ #
50
+ # To customize this provide a translation for the key:
51
+ # `uchi.breadcrumb.root.label`
52
+ def breadcrumb_label_for_root
53
+ first_present_value(
54
+ translate("breadcrumb.root.label", default: nil),
55
+ Rails.application.name.titlecase
56
+ )
57
+ end
58
+
47
59
  # Returns a description for the given page, or nil if none is found.
48
60
  # This description is intended to provide additional context for the page
49
61
  # being shown.
@@ -126,30 +138,53 @@ module Uchi
126
138
  end
127
139
 
128
140
  def link_to_destroy(record)
129
- translate(
130
- "link_to_destroy",
131
- default: "Delete",
132
- model: singular_name,
133
- record: repository.title(record),
134
- scope: i18n_scope("button")
141
+ first_present_value(
142
+ translate(
143
+ "link_to_destroy",
144
+ default: nil,
145
+ model: singular_name,
146
+ record: repository.title(record),
147
+ scope: i18n_scope("button")
148
+ ),
149
+ translate(
150
+ "common.destroy",
151
+ default: nil,
152
+ model: singular_name,
153
+ record: repository.title(record)
154
+ ),
155
+ "Delete"
135
156
  )
136
157
  end
137
158
 
138
159
  def link_to_edit(record)
139
- translate(
140
- "link_to_edit",
141
- default: "Edit",
142
- model: singular_name,
143
- record: repository.title(record),
144
- scope: i18n_scope("button")
160
+ first_present_value(
161
+ translate(
162
+ "link_to_edit",
163
+ default: nil,
164
+ model: singular_name,
165
+ record: repository.title(record),
166
+ scope: i18n_scope("button")
167
+ ),
168
+ translate(
169
+ "common.edit",
170
+ default: nil,
171
+ model: singular_name,
172
+ record: repository.title(record)
173
+ ),
174
+ "Edit"
145
175
  )
146
176
  end
147
177
 
148
178
  # Returns the text for the "new" action link.
179
+ #
180
+ # Returns the first of the following translations that is present:
181
+ # 1. Translation from "uchi.repository.[name].button.link_to_new"
182
+ # 2. Translation from "uchi.common.new" with interpolation key %{model}
183
+ # 3. Default string "New %{model}"
149
184
  def link_to_new
150
185
  translate(
151
186
  "link_to_new",
152
- default: "New %{model}", # rubocop:disable Style/FormatStringToken
187
+ default: translate("common.new", default: "New %{model}"),
153
188
  model: singular_name,
154
189
  scope: i18n_scope("button")
155
190
  )
@@ -159,6 +194,21 @@ module Uchi
159
194
  translate("loading", default: "Loading...", scope: "uchi.repository.common")
160
195
  end
161
196
 
197
+ # Returns the label for the navigation link to this repository's index
198
+ # page.
199
+ #
200
+ # Returns the first of the following that is present:
201
+ # 1. Translation from "uchi.repository.[name].navigation.label"
202
+ # 2. Translation from "uchi.repository.[name].index.title"
203
+ # 3. plural name of the model
204
+ def navigation_label
205
+ first_present_value(
206
+ translate(i18n_scope("navigation.label"), default: nil),
207
+ translate(i18n_scope("index.title"), default: nil),
208
+ plural_name
209
+ )
210
+ end
211
+
162
212
  # Returns the localized, human-readable plural name of the model this
163
213
  # repository manages.
164
214
  def plural_name
@@ -222,16 +272,50 @@ module Uchi
222
272
  )
223
273
  end
224
274
 
225
- # Returns the title for the given page.
226
- def title(page, record: nil)
227
- return repository.title(record) if record && page == :show
275
+ def title_for_edit(record)
276
+ first_present_value(
277
+ translate(
278
+ "title",
279
+ default: nil,
280
+ model: singular_name,
281
+ record: repository.title(record),
282
+ scope: i18n_scope(:edit)
283
+ ),
284
+ link_to_edit(record)
285
+ )
286
+ end
228
287
 
288
+ def title_for_index
229
289
  translate(
230
290
  "title",
231
291
  default: plural_name,
232
292
  model: singular_name,
233
- record: record,
234
- scope: i18n_scope(page)
293
+ scope: i18n_scope(:index)
294
+ )
295
+ end
296
+
297
+ def title_for_show(record)
298
+ return repository.title(record) if record
299
+
300
+ translate(
301
+ "title",
302
+ default: plural_name,
303
+ model: singular_name,
304
+ scope: i18n_scope(:show)
305
+ )
306
+ end
307
+
308
+ # Returns the title for the "new" page.
309
+ #
310
+ # Returns the first of the following translations that is present:
311
+ # 1. Translation from "uchi.repository.[name].new.title"
312
+ # 2. Translation from "uchi.repository.[name].button.link_to_new"
313
+ # 3. Translation from "uchi.common.new" with interpolation key %{model}
314
+ # 4. Default string "New %{model}"
315
+ def title_for_new
316
+ first_present_value(
317
+ translate(i18n_scope("new.title"), default: nil),
318
+ link_to_new
235
319
  )
236
320
  end
237
321
 
data/lib/uchi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uchi
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Skjerning