active_scaffold_child_memberships 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 695eb1a91c4345770c5bc16711996ad375555906d762d191b8bd9b18d93ba063
4
- data.tar.gz: 9e0c2123bff4f5de59075d4de5d298fa9123cffcb33a1587de0fbf216b61d46e
3
+ metadata.gz: dcae2e51da5f8fbd6657b07a650b7b2b14af07a2fa5050c7ef1da0efff40d2ca
4
+ data.tar.gz: 448d1630e63518f9a70ac0470ba274731d07774a1edc7fc8671f1a58a6458bab
5
5
  SHA512:
6
- metadata.gz: afc624967c0eb0f8d69e8a040cc601f18ee39a7752513513d863a54d0e84dced301d00aa3310f51ec05da9bee9d660ed977eb7bf2e0281ce00fed73bccf6e9f3
7
- data.tar.gz: 658231e5a370393e870cd84432c3bbab28a4b20c314a37f1c0f33d7a564a09501edca8dabd45a41a35fcae15e6fe08beadbd9e48eef5522a18684e86d1ef4ab9
6
+ metadata.gz: 1456b4e117597f4d3f113d27df2baca7b6f780d37586aa8a3e7781cb5106344c3ce2503b82651c75df54a0bb1a6ab7cd5c002df332072069be0b7c843beed40b
7
+ data.tar.gz: b6783c577d011dd4a011e9589e22b1cc398038402aeffba7cc1d86b13c5c93d04b73ddf6c6315736c56fa5a53dccad8fcb28234d3d5d828fa459c844b8cb4101
data/README.md CHANGED
@@ -6,3 +6,127 @@ It requires ActiveScaffold 4.0, and RecordSelect 4.0.2 if want to use :recordsel
6
6
 
7
7
  ## Usage
8
8
 
9
+ ActiveScaffold allows to edit memberships on one record with :select form_ui, for either has_and_belongs_to_many, or has_many through. For example, using :select in :roles column with any of these examples would work in the same way:
10
+
11
+ ```ruby
12
+ class User < ApplicationRecord
13
+ has_and_belongs_to_many :roles
14
+ end
15
+
16
+ class User < ApplicationRecord
17
+ has_many :role_memberships
18
+ has_many :roles, through: :role_memberships
19
+ end
20
+
21
+ class RoleMembership < ApplicationRecord
22
+ belongs_to :user
23
+ belongs_to :role
24
+ end
25
+ ```
26
+
27
+ But sometimes you may have User model grouped in other model, with has_many association, and may want to edit memberships for all the users in the group.
28
+
29
+ ```ruby
30
+ class Group < ApplicationRecord
31
+ has_many :users
32
+ end
33
+ ```
34
+
35
+ This form_ui :child_memberships can be used in a :roles has_many through association:
36
+
37
+ ```ruby
38
+ class Group < ApplicationRecord
39
+ has_many :users
40
+ has_many :roles, through: :users
41
+ end
42
+ class GroupsController < ApplicationController
43
+ active_scaffold :group do |conf|
44
+ conf.update.columns = [:name, :roles]
45
+ conf.columns[:roles].form_ui = :child_memberships
46
+ end
47
+ end
48
+ ```
49
+
50
+ Then you get a table, with all users in the group in the rows, and roles in the columns. Only the assigned roles are in the columns by default, and new column can be added with a select tag to pick a new role to assign to users:
51
+
52
+ ![image](https://github.com/user-attachments/assets/793b07bd-b7bf-4336-a319-b40136cdb5e8)
53
+
54
+ The form_ui used to select the record of the new columns can be changed, with options for the form_ui (or column's options), with the key `:add_column_ui`, for example to use RecordSelect:
55
+
56
+ ```ruby
57
+ conf.columns[:roles].form_ui = :child_memberships, {add_column_ui: :record_select}
58
+ ```
59
+
60
+ If no form_ui is defined with `:add_column_ui`, it defaults to `:select`. If ui options must be passed to the form_ui, they can be added using an array with the form UI and the options hash:
61
+
62
+ ```ruby
63
+ conf.columns[:roles].form_ui = :child_memberships, {add_column_ui: [:select, {include_blank: 'Pick one'}]}
64
+ ```
65
+
66
+ The method used for the label in the rows is defined with `:label_method`, defaults to `:to_label`. For example, to use `short_name` method of Role model:
67
+
68
+ ```ruby
69
+ conf.columns[:roles].form_ui = :child_memberships, {label_method: :short_name}
70
+ ```
71
+
72
+ The method used for the label in the rows is defined with `:child_label_method`, defaults to `:to_label`. For example, to use `first_name` method of User model:
73
+
74
+ ```ruby
75
+ conf.columns[:roles].form_ui = :child_memberships, {child_label_method: :first_name}
76
+ ```
77
+
78
+ The label of the link to add a new column uses `:add_label` translation key, in the `:active_scaffold` scope. It can be changed to a string, skipping the translation, or other symbol to use other translation key, in the `:active_scaffold` scope. The translation can use `%{model}` variable to display the name of the associated model.
79
+
80
+ ```ruby
81
+ conf.columns[:roles].form_ui = :child_memberships, {add_label: 'Add other role'}
82
+ ```
83
+
84
+ If new columns are not allowed, it can be disabled using `add_label: false`:
85
+
86
+ ```ruby
87
+ conf.columns[:roles].form_ui = :child_memberships, {add_label: false}
88
+ ```
89
+
90
+ ## Overriding Helpers
91
+
92
+ The helper `active_scaffold_child_memberships_members` can be overrided to change the default columns rendered, instead of the assigned records only. For example to render all available roles:
93
+
94
+ ```ruby
95
+ def active_scaffold_child_memberships_helper(column, row_records)
96
+ column.association.klass.all
97
+ end
98
+ ```
99
+
100
+ The helper can use model prefix too:
101
+
102
+ ```ruby
103
+ def group_active_scaffold_child_memberships_helper(column, row_records)
104
+ column.association.klass.all
105
+ end
106
+ ```
107
+
108
+ To change the record selector for new columns, the helper `active_scaffold_child_memberships_new_column` can be overrided instead of using `:add_column_ui`. It supports model prefix too. The field should be rendered disabled, as it's a template to be cloned when the add column link is clicked:
109
+
110
+ ```ruby
111
+ def active_scaffold_child_memberships_new_column(column, name, source_col, ui_options)
112
+ if column == :roles
113
+ select_tag name, options_from_collection_for_select(Role.allowed_for(current_login), :id, :name), id: nil, disabled: true
114
+ else
115
+ super
116
+ end
117
+ end
118
+ ```
119
+
120
+ Also, the available records can be changed with the usual methods, `options_for_association_conditions` and `association_klass_scoped`, they receive the source association of the has_many through association (`:roles` in the example), and an empty record of the through association.
121
+
122
+ To change how checkboxes are rendered, for example, adding extra content, the helper `active_scaffold_child_memberships_checkbox` can be overrided. It supports model prefix too. The argument child is the record of the row, and member is the record of the column.
123
+
124
+ ```ruby
125
+ def active_scaffold_child_memberships_checkbox(column, source_column, child, member, name:, value:)
126
+ if column == :roles
127
+ safe_join [super, content_tag(:span, member.state_for(child))]
128
+ else
129
+ super
130
+ end
131
+ end
132
+ ```
@@ -0,0 +1,3 @@
1
+ en:
2
+ active_scaffold:
3
+ add_column: Add %{model}
@@ -0,0 +1,3 @@
1
+ es:
2
+ active_scaffold:
3
+ add_column: Añadir %{model}
@@ -15,9 +15,11 @@ module ActiveScaffoldChildMemberships
15
15
  end
16
16
  header.unshift content_tag(:th, through_col.label)
17
17
 
18
+ checkbox_helper = override_helper_per_model(:active_scaffold_child_membership_checkbox, column.active_record_class)
18
19
  rows = children.map do |record|
20
+ checkbox_name = "#{options[:name]}[memberships][#{record.id}][]"
19
21
  columns = column_records.map.with_index do |member, i|
20
- content_tag(:td, check_box_tag("#{options[:name]}[memberships][#{record.id}][]", i, record.send(source_col.name).include?(member), id: nil))
22
+ content_tag(:td, send(checkbox_helper, column, source_col, record, member, name: checkbox_name, value: i))
21
23
  end
22
24
  label = record.send(ui_options[:child_label_method] || :to_label)
23
25
  columns.unshift content_tag(:td, h(label))
@@ -42,7 +44,10 @@ module ActiveScaffoldChildMemberships
42
44
 
43
45
  def active_scaffold_child_memberships_members(column, row_records)
44
46
  row_records.flat_map(&column.association.source_reflection.name).uniq
45
- #column.association.klass.all
47
+ end
48
+
49
+ def active_scaffold_child_membership_checkbox(column, source_column, child, member, name:, value:)
50
+ check_box_tag(name, value, child.send(source_column.name).include?(member), id: nil)
46
51
  end
47
52
 
48
53
  def active_scaffold_child_memberships_new_column(column, name, source_col, ui_options)
@@ -2,7 +2,7 @@ module ActiveScaffoldChildMemberships
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 0
5
+ PATCH = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_scaffold_child_memberships
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Cambra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-16 00:00:00.000000000 Z
11
+ date: 2025-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_scaffold
@@ -35,6 +35,8 @@ files:
35
35
  - LICENSE
36
36
  - README.md
37
37
  - app/assets/javascripts/active_scaffold_child_memberships.js
38
+ - config/locales/en.yml
39
+ - config/locales/es.yml
38
40
  - lib/active_scaffold_child_memberships.rb
39
41
  - lib/active_scaffold_child_memberships/attribute_params.rb
40
42
  - lib/active_scaffold_child_memberships/engine.rb