active_scaffold_child_memberships 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +112 -0
- data/config/locales/en.yml +3 -0
- data/config/locales/es.yml +3 -0
- data/lib/active_scaffold_child_memberships/helpers.rb +0 -1
- data/lib/active_scaffold_child_memberships/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ed22261d2b7d6a07b9f61b19b419bf4ff53491b57671aa5e702f629f41d1b25
|
4
|
+
data.tar.gz: f70a457b8bdfbfe185141a861ee6e1b91cd4960953148e8cdffbff273ec8e56c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a183a7b1b809c43b2513aba3b85f595c7f3a559bd5d1cd86004dd572639b4b1473cd2745cf8b32a21c61c02a21d14e3ef9162d6e109ac0803dbc19fe7aa299a
|
7
|
+
data.tar.gz: 78bc6ea3fc431364211180efbc05524eba8e1215d807853447034af896fe17422dbc243559151f651ea242f35247e221553632aa66f96154113061e781c7c45e
|
data/README.md
CHANGED
@@ -6,3 +6,115 @@ 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_helper` 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.
|
@@ -42,7 +42,6 @@ module ActiveScaffoldChildMemberships
|
|
42
42
|
|
43
43
|
def active_scaffold_child_memberships_members(column, row_records)
|
44
44
|
row_records.flat_map(&column.association.source_reflection.name).uniq
|
45
|
-
#column.association.klass.all
|
46
45
|
end
|
47
46
|
|
48
47
|
def active_scaffold_child_memberships_new_column(column, name, source_col, ui_options)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_scaffold_child_memberships
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergio Cambra
|
@@ -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
|