completion-kit 0.27.1 → 0.27.2

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: 38c7acaadacc7cb636ee99e3eb7e4c52f68b89ed57122487eb1591be99435716
4
- data.tar.gz: 3e5f9c36019c6635ae41b913eea74607f3b8c9f21a511c5ccc3f9ff75422282d
3
+ metadata.gz: 7f8fdeef847d7243a665b86e111343d0255ac2eb56ba6538c56c21871de04d1c
4
+ data.tar.gz: 58911d7d679a943284eacaaca586c6ad375efdf674aa1604f9895ca971f80c2e
5
5
  SHA512:
6
- metadata.gz: 80adc55bde36fa3ac782c213c4786e677cd27d1b85bef81c14fbd89e05a82e229681866390c34d7f17cd9d6a153bcb93da705d63a87f7aede39dcb26929e3629
7
- data.tar.gz: 533ef4f6c4b29791a66ce9169ab4c980a38838f5e3a5184d0b07cf0a4276ad0fd5990fae69616a2ea43421daebab1ca1ed2f246b4a310a506403db7548935981
6
+ metadata.gz: fd44b864ef07239759b9211cf24dac1aad601d30bd1b92068e5e2952571e338decb85be400464c1c3a947f40040c14aeea74384512594290f79dbfddbed19555
7
+ data.tar.gz: ad879e8ebc9f77dab47592f23b3bf0a6ae7eb101e11dc07e43a1ed7b67caa19fde109b3dcc20ab445c3a0c4daebb843b442b1de69ec9e433a3fe5da9d7d5c661
@@ -25,12 +25,11 @@ module CompletionKit
25
25
 
26
26
  def update
27
27
  if @prompt.runs.exists?
28
- new_prompt = @prompt.build_next_version(prompt_params.except(:tag_names).to_h)
28
+ new_prompt = @prompt.build_next_version(prompt_params.to_h)
29
29
  if new_prompt.valid?
30
30
  CompletionKit::ApplicationRecord.transaction do
31
31
  new_prompt.save!
32
32
  new_prompt.publish!
33
- new_prompt.update!(tag_names: prompt_params[:tag_names]) if prompt_params.key?(:tag_names)
34
33
  end
35
34
  render json: new_prompt.reload
36
35
  else
@@ -33,16 +33,15 @@ module CompletionKit
33
33
 
34
34
  def update
35
35
  if @prompt.runs.exists?
36
- new_prompt = @prompt.build_next_version(prompt_params.except(:tag_names).to_h)
36
+ new_prompt = @prompt.build_next_version(prompt_params.to_h)
37
37
  if new_prompt.valid?
38
38
  CompletionKit::ApplicationRecord.transaction do
39
39
  new_prompt.save!
40
40
  new_prompt.publish!
41
- new_prompt.update!(tag_names: prompt_params[:tag_names]) if prompt_params.key?(:tag_names)
42
41
  end
43
42
  redirect_to prompt_path(new_prompt), notice: "Saved as #{new_prompt.version_label}."
44
43
  else
45
- @prompt.assign_attributes(prompt_params.except(:tag_names).to_h)
44
+ @prompt.assign_attributes(prompt_params.to_h)
46
45
  @prompt.errors.merge!(new_prompt.errors)
47
46
  render :edit, status: :unprocessable_entity
48
47
  end
@@ -7,18 +7,42 @@ module CompletionKit
7
7
  class_name: "CompletionKit::Tagging",
8
8
  dependent: :destroy
9
9
  has_many :tags, through: :taggings, class_name: "CompletionKit::Tag"
10
+
11
+ validate :assigned_tag_names_are_valid, if: -> { @tag_names_assigned }
12
+ after_save :sync_assigned_tags, if: -> { @tag_names_assigned }
10
13
  end
11
14
 
12
15
  def tag_names
16
+ return @assigned_tag_names if @tag_names_assigned
17
+
13
18
  tags.pluck(:name)
14
19
  end
15
20
 
16
21
  def tag_names=(names)
17
- resolved = Array(names)
22
+ @tag_names_assigned = true
23
+ @assigned_tag_names = Array(names)
18
24
  .map { |n| n.to_s.strip.downcase }
19
25
  .reject(&:blank?)
20
26
  .uniq
21
- self.tags = resolved.map { |name| CompletionKit::Tag.find_or_create_by!(name: name) }
27
+ end
28
+
29
+ private
30
+
31
+ def assigned_tag_names_are_valid
32
+ @assigned_tag_names.each do |name|
33
+ next if CompletionKit::Tag.exists?(name: name)
34
+
35
+ probe = CompletionKit::Tag.new(name: name)
36
+ next if probe.valid?
37
+
38
+ errors.add(:base, "Tag \"#{name}\" is not allowed: #{probe.errors[:name].to_sentence}")
39
+ end
40
+ end
41
+
42
+ def sync_assigned_tags
43
+ self.tags = @assigned_tag_names.map { |name| CompletionKit::Tag.find_or_create_by!(name: name) }
44
+ @tag_names_assigned = false
45
+ @assigned_tag_names = nil
22
46
  end
23
47
  end
24
48
  end
@@ -2,15 +2,21 @@
2
2
  <% label_text = local_assigns[:label].presence || "#{param_namespace.to_s.titleize} tags" %>
3
3
  <%= label_tag "#{param_namespace}_tags", label_text, class: "ck-label" %>
4
4
  <% all_tags = CompletionKit::Tag.order(:name) %>
5
- <% selected_ids = (record.persisted? || record.tags.loaded?) ? record.tags.map(&:id) : [] %>
5
+ <% selected_names = record.tag_names %>
6
6
  <div class="ck-tag-picker">
7
7
  <% all_tags.each do |tag| %>
8
- <% checked = selected_ids.include?(tag.id) %>
8
+ <% checked = selected_names.include?(tag.name) %>
9
9
  <label class="tag-mark" style="--mark-color: var(--tag-<%= tag.color %>);">
10
10
  <%= check_box_tag "#{param_namespace}[tag_names][]", tag.name, checked, class: "ck-visually-hidden", "aria-label": "Tag: #{tag.name}" %>
11
11
  <span aria-hidden="true"><%= tag.name %></span>
12
12
  </label>
13
13
  <% end %>
14
+ <% (selected_names - all_tags.map(&:name)).each do |pending_name| %>
15
+ <label class="tag-mark" style="--mark-color: var(--ck-muted);">
16
+ <%= check_box_tag "#{param_namespace}[tag_names][]", pending_name, true, class: "ck-visually-hidden", "aria-label": "Tag: #{pending_name}" %>
17
+ <span aria-hidden="true"><%= pending_name %></span>
18
+ </label>
19
+ <% end %>
14
20
  <%= text_field_tag "#{param_namespace}[tag_names][]", "",
15
21
  id: "#{param_namespace}_tags",
16
22
  class: "ck-tag-picker__input",
@@ -1,3 +1,3 @@
1
1
  module CompletionKit
2
- VERSION = "0.27.1"
2
+ VERSION = "0.27.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: completion-kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.1
4
+ version: 0.27.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Bastin