active_scaffold_kanban 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e25b955b58cdedd3c53158c2c0750756e9939033ca0622e14b5e5a74f065ade3
4
+ data.tar.gz: 6e2809f0242b81621ced3742b04c16d0a288568f6dca7c3f5df88f2448983519
5
+ SHA512:
6
+ metadata.gz: 61313ca7f2eeaf6317b87f22cb9a526caa48a9406a7c9789503742f7c059a9813d05ffe9ee88496c26cbdb07ad830fef423e177dfc3898a82a3d4937e12337d9
7
+ data.tar.gz: ed1fde6d9d9f84c8a5473fb122067a568a6b1e8831f846c939c15b41e132d5f30694f14c4c14461ce52bd5f5fe4a8741502a97db42f9e06b51b32da222746357
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sergio Cambra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Kanban for ActiveScaffold
2
+
3
+ An addon for ActiveScaffold to render a kanban board instead of normal list, using a model's column for the kanban columns.
4
+
5
+ ## Usage
6
+
7
+ Add :kanban to actions.
8
+
9
+ ```rb
10
+ active_scaffold :model do |conf|
11
+ conf.actions << :kanban
12
+ ```
13
+
14
+ Kanban view is used when index action is loaded with `view=kanban` parameter. Default list view can be replaced with kanban with `conf.kanban.replace_list_view = true`.
15
+
16
+ Then define the column used as kanban columns in the board with `conf.kanban.group_by_column`. It works with both DB column and association.
17
+ The columns for the board are returned with the `kanban_columns` helper, and it uses the same way to get the values as :select form_ui. The helper `kanban_columns` can be overrided, supporting model prefix, but the default helper supports the same methods to define or change the available values:
18
+ - for single columns, define with `options[:options]` in the column, or override `active_scaffold_enum_options` (supports model prefix)
19
+ - for associations, define `options_for_association_conditions` or `association_klass_scoped`, model prefix is supported too.
20
+
21
+ Dragging a card to other column will use `update_column` action, like inplace edit, to change the value. If `update_column` action fails to save, the card is reverted to the original position.
22
+
23
+ If no position column is defined in sortable, or sortable action is not added, the position on the column can't be changed (drop on the same column reverts to the original position), and the position on the new column is not respected (position may change when kanban is reloaded).
24
+
25
+ To define a position column, add `:sortable` to `conf.actions`, and define `conf.sortable.column`, then changing the position on the original column will use `reorder` action (as active_scaffold_sortable), and dragging to other column will use `update_action` and will pass the new order to update the order too.
26
+
27
+ A column may accept items, but don't allow to drag items out. Override `kanban_column_receive_only?` (supports model prefix too) which receives the column value (associated record if the column is an association) and return true for the columns which don't accept to move cards out.
28
+
29
+ Define the method used for the title with conf.kanban.title_method (defaults to :to_label), and method used for description with conf.kanban.description_method. The card is rendered with `_kanban_card.html.erb` view partial, which can be overrided to change the html structure of a card, and supports model prefix too. Also the `kanban_description` helper can be defined, which supports model prefix, to change only the body of the card. The actions are rendered calling the `kanban_actions` helper, which supports model prefix, or actions can be ignored in kanban by defining ignore_method? in the action_link to skip them if `@kanban_view` variable is set, or overriding `skip_action_link?` helper.
30
+
31
+ A javascript event `kanban:beforeChange` is fired on the card, when it's drop into other column, before sending the request to update the column. The event receives a object argument with 2 properties, the id of the card and the value of the new column (when column is an association, value is an id too). If the event listener returns false, it will reject the change, and the card will revert to the original position. The event listener can set extra params to send to the `update_column` action, with `jQuery(event.target).data('params', {key: value})`.
@@ -0,0 +1,100 @@
1
+ ActiveScaffold.update_positions = function(content) {
2
+ if (typeof(content) == 'string') content = jQuery('#' + content);
3
+ var element = content.closest('.sortable-container');
4
+ jQuery.each(content.find('.sub-form-record input[name$="[' + element.data('column') + ']"]'), function(i, field) {
5
+ jQuery(field).val(i+1); // don't use 0
6
+ });
7
+ }
8
+ ActiveScaffold.kanban = function(container) {
9
+ jQuery.each(jQuery('.kanban', container), function(i, kanban) {
10
+ kanban = jQuery(kanban);
11
+
12
+ var revert = function(item) {
13
+ var prev = jQuery(item).data('prev_card');
14
+ if (prev.length) prev.after(item);
15
+ else jQuery(item).data('prev_column').find('.cards').prepend(item);
16
+ };
17
+ var reorder_info = function(column) {
18
+ var kanban = column.closest('.kanban');
19
+ return column.sortable('serialize', {
20
+ key: encodeURIComponent(kanban.data('reorder-key') + '[]'),
21
+ expression: new RegExp(kanban.data('format'))
22
+ });
23
+ }
24
+
25
+ var reorder_url = kanban.data('reorder-url'),
26
+ change_url = kanban.data('change-url'),
27
+ options = {
28
+ connectWith: '.active-scaffold .kanban .kanban-column .cards',
29
+ cancel: '.kanban-column[data-receive-only] .card',
30
+ start: function(event, ui) {
31
+ ui.item.data('prev_column', ui.item.closest('.kanban-column'))
32
+ .data('prev_card', ui.item.prev());
33
+ },
34
+ beforeStop: function(event, ui) {
35
+ event.handleObj.data = jQuery.extend(event.handleObj.data, {column_changed: ui.item.closest('.cards').get(0) !== this});
36
+ if (!reorder_url) ui.item.cancel = !event.handleObj.data.column_changed;
37
+ },
38
+ update: function(event, ui) {
39
+ if (ui.item.cancel) jQuery(this).sortable('cancel');
40
+ else if (!event.handleObj.data || !event.handleObj.data.column_changed) {
41
+ jQuery.ajax({
42
+ url: reorder_url,
43
+ data: reorder_info(jQuery(this)),
44
+ method: 'POST',
45
+ context: ui.item,
46
+ error: function(xhr, status, error) {
47
+ if (xhr.status >= 400) revert(this);
48
+ }
49
+ });
50
+ }
51
+ },
52
+ over: function(event, ui) {
53
+ jQuery(this).addClass('ui-highlight');
54
+ },
55
+ out: function(event, ui) {
56
+ jQuery(this).removeClass('ui-highlight');
57
+ },
58
+ receive: function(event, ui) {
59
+ var $this = jQuery(this), id = ui.item.data('value'),
60
+ column_value = $this.closest('.kanban-column').data('value'),
61
+ data = {value: column_value};
62
+ if (jQuery.rails.fire(ui.item, 'kanban:beforeChange', {id: id, column: column_value})) {
63
+ jQuery.extend(data, ui.item.data('params') || {});
64
+ if (reorder_url) data = jQuery.param(data) + '&' + reorder_info($this);
65
+ jQuery.ajax({
66
+ url: change_url.replace('--ID--', id),
67
+ data: data,
68
+ method: 'POST',
69
+ context: ui.item,
70
+ error: function(xhr, status, error) {
71
+ if (xhr.status >= 400) revert(this);
72
+ }
73
+ });
74
+ } else revert(ui.item);
75
+ }
76
+ };
77
+ kanban.find('.kanban-column .cards').sortable(options);
78
+ return;
79
+ if (url) {
80
+ var csrf = jQuery('meta[name=csrf-param]').attr('content') + '=' + jQuery('meta[name=csrf-token]').attr('content');
81
+ sortable_options.update = function(event, ui) {
82
+ var $this = jQuery(this),
83
+ body = $this.sortable('serialize',{key: encodeURIComponent(($this.data('key') || $this.attr('id')) + '[]'), expression: new RegExp(element.data('format'))});
84
+ var params = element.data('with');
85
+ if (params) body += '&' + params;
86
+ jQuery.post(url, body + '&' + csrf);
87
+ };
88
+ }
89
+ });
90
+ };
91
+
92
+ jQuery(document).ready(function($) {
93
+ $(document).on('as:action_success', 'a.as_action', function(e, action_link) {
94
+ ActiveScaffold.kanban(action_link.adapter);
95
+ });
96
+ $(document).on('as:element_updated', function(e) {
97
+ ActiveScaffold.kanban(e.target);
98
+ });
99
+ ActiveScaffold.kanban(document);
100
+ });
@@ -0,0 +1,64 @@
1
+ .active-scaffold .kanban {
2
+ display: flex;
3
+ flex-direction: row;
4
+
5
+ .kanban-column {
6
+ display: flex;
7
+ flex-direction: column;
8
+ margin-left: 1em;
9
+ &:first-child {
10
+ margin-left: 0;
11
+ }
12
+
13
+ .title {
14
+ margin-bottom: 0.5em;
15
+ }
16
+
17
+ .cards {
18
+ width: 15em;
19
+ height: 100%;
20
+ padding: 4px;
21
+ border-radius: 5px;
22
+ &.ui-highlight {
23
+ background-color: #f8f8f8;
24
+ }
25
+ }
26
+
27
+ &:not([data-receive-only]) .ui-sortable .card {
28
+ cursor: grab;
29
+ &.ui-sortable-helper {
30
+ cursor: grabbing;
31
+ }
32
+ }
33
+
34
+ .card {
35
+ margin-top: 0.5em;
36
+ border: 1px solid #555;
37
+ border-radius: 5px;
38
+ padding: 0.2em 0.4em;
39
+
40
+ .card-title {
41
+ position: relative;
42
+
43
+ .actions {
44
+ position: absolute;
45
+ display: none;
46
+ right: 0;
47
+ top: 1.5em;
48
+ background: #fff;
49
+ border: 1px solid #bbb;
50
+ }
51
+ &:hover .actions {
52
+ display: block;
53
+ }
54
+ }
55
+
56
+ .card-description {
57
+ font-size: 90%;
58
+ padding-top: 0.5em;
59
+ margin-top: 0.5em;
60
+ border-top: 1px solid #aaa;
61
+ }
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,16 @@
1
+ <% column_receive_only_method = override_helper_per_model(:kanban_column_receive_only?, active_scaffold_config.model) %>
2
+ <%= content_tag :div, class: 'kanban', data: kanban_data_attrs do %>
3
+ <% send(override_helper_per_model(:kanban_columns, active_scaffold_config.model)).each do |label, value| %>
4
+ <%
5
+ id = value.id if @kanban_column.association
6
+ column_data = {value: id || value}
7
+ column_data[:receive_only] = true if send(column_receive_only_method, value)
8
+ %>
9
+ <%= content_tag :div, class: 'kanban-column', data: column_data do %>
10
+ <div class="title"><%= label %></div>
11
+ <div class="cards">
12
+ <%= render partial: 'kanban_card', as: :record, collection: @records.select { |record| record.send(@kanban_column.name) == value } %>
13
+ </div>
14
+ <% end %>
15
+ <% end %>
16
+ <% end %>
@@ -0,0 +1,12 @@
1
+ <% @kanban_card_body_method ||= override_helper_per_model(:kanban_description, record.class) %>
2
+ <div class="card record" data-value="<%= record.id %>" id="<%= element_row_id(:action => :list, :id => record.id) %>">
3
+ <div class="card-title">
4
+ <%= record.send(active_scaffold_config.kanban.title_method) %>
5
+ <div class="actions">
6
+ <%= display_action_links(active_scaffold_config.action_links.member, record, for: record) %>
7
+ </div>
8
+ </div>
9
+ <div class="card-description">
10
+ <%= send(@kanban_card_body_method, record) %>
11
+ </div>
12
+ </div>
@@ -0,0 +1,5 @@
1
+ <% if @kanban_view %>
2
+ <%= render 'kanban' %>
3
+ <% else %>
4
+ <%= render :super %>
5
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <% if @kanban_view %>
2
+ <%= selector = ".kanban-column[data-value=\"#{@record.send(@kanban_column.name)}\"] .cards" %>
3
+ // ActiveScaffold.create_record_row('<%= active_scaffold_id %>', html, <%= {insert_at: insert_at, body_selector: selector}.to_json.html_safe %>);
4
+ jQuery('<%= selector %>').prepend('<%= escape_javascript render('kanban_card', record: @record) %>');
5
+ jQuery('<%= selector %>').sortable('refresh');
6
+ <% else %>
7
+ <%= render :super %>
8
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <% if @kanban_view %>
2
+ <% if successful? %>
3
+ ActiveScaffold.replace('<%= element_row_id(action: :list) %>', '<%= escape_javascript render('kanban_card', record: @record) %>');
4
+ <% end %>
5
+ <% else %>
6
+ <%= render :super %>
7
+ <% end %>
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveScaffold
4
+ module Actions
5
+ module Kanban
6
+ def self.included(base)
7
+ base.before_action :setup_kanban, only: [:index, :update_column, :reorder, :create]
8
+ base.after_action :reorder_cards, only: :update_column
9
+ base.after_action :set_error_status_code, only: [:update_column, :reorder]
10
+ end
11
+
12
+ protected
13
+
14
+ def setup_kanban
15
+ return unless params[:view] == 'kanban' || active_scaffold_config.kanban.replace_list_view
16
+ @kanban_view = true
17
+ @kanban_column = active_scaffold_config.columns[active_scaffold_config.kanban.group_by_column]
18
+ active_scaffold_config.list.pagination = nil
19
+ end
20
+
21
+ def reorder_cards
22
+ reorder if successful? && active_scaffold_config.actions.include?(:sortable) && ordered_ids
23
+ end
24
+
25
+ def set_error_status_code
26
+ response.status = 400 unless successful?
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveScaffold
4
+ module Config
5
+ class Kanban < Base
6
+ self.crud_type = :read
7
+
8
+ def initialize(core_config)
9
+ super
10
+ @title_method = self.class.title_method
11
+ @description_method = self.class.description_method
12
+ @replace_list_view = self.class.replace_list_view
13
+ end
14
+
15
+ # global level configuration
16
+ # --------------------------
17
+ # the model's method used for card's title
18
+ cattr_accessor :title_method, instance_accessor: false
19
+ @@title_method = :to_label
20
+
21
+ # the model's method used for card's description, set to nil for no description
22
+ cattr_accessor :description_method, instance_accessor: false
23
+
24
+ # enable it to replace list view with kanban, instead of being optional with view=kanban param
25
+ cattr_accessor :replace_list_view, instance_accessor: false
26
+
27
+ # the model's method used for card's title
28
+ attr_accessor :title_method
29
+
30
+ # the model's method used for card's description, set to nil for no description
31
+ attr_accessor :description_method
32
+
33
+ # enable it to replace list view with kanban, instead of being optional with view=kanban param
34
+ attr_accessor :replace_list_view
35
+
36
+ # the model's column used for kanban columns
37
+ attr_accessor :group_by_column
38
+
39
+ UserSettings.class_eval do
40
+ user_attr :title_method, :description_method, :group_by_column, :replace_list_view
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveScaffold
2
+ module Helpers
3
+ module KanbanHelpers
4
+ def kanban_columns
5
+ record = active_scaffold_config.model.new
6
+ if @kanban_column.association
7
+ sorted_association_options_find(@kanban_column.association, nil, record).map do |record_column|
8
+ [record_column.send(@kanban_column.options[:label_method] || :to_label), record_column]
9
+ end
10
+ else
11
+ enum_options_method = override_helper_per_model(:active_scaffold_enum_options, record.class)
12
+ send(enum_options_method, @kanban_column, record).collect do |text, value|
13
+ text, value = active_scaffold_translated_option(@kanban_column, text, value)
14
+ value = value.to_s if value.is_a? Symbol
15
+ [text, value]
16
+ end
17
+ end
18
+ end
19
+
20
+ def kanban_column_receive_only?(column_value)
21
+ false
22
+ end
23
+
24
+ def kanban_description(record)
25
+ record.send(active_scaffold_config.kanban.description_method) if active_scaffold_config.kanban.description_method
26
+ end
27
+
28
+ def kanban_data_attrs
29
+ attrs = {
30
+ change_url: url_for(params_for(action: :update_column, id: '--ID--', column: active_scaffold_config.kanban.group_by_column)),
31
+ format: '^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$',
32
+ reorder_key: active_scaffold_tbody_id
33
+ }
34
+ if active_scaffold_config.actions.include?(:sortable) && active_scaffold_config.sortable.column
35
+ attrs[:reorder_url] = url_for(params_for(action: :reorder, id: nil))
36
+ end
37
+ attrs
38
+ end
39
+
40
+ def action_link_html_options(link, record, options)
41
+ options = super
42
+ if @kanban_view && link.position == :after
43
+ options[:data][:position] = :replace
44
+ end
45
+ options
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveScaffoldKanban
2
+ class Engine < ::Rails::Engine
3
+ initializer 'active_scaffold_kanban.action_view' do
4
+ ActiveSupport.on_load :action_view do
5
+ ActionView::Base.send :include, ActiveScaffold::Helpers::KanbanHelpers
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveScaffoldKanban
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+
7
+ STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_scaffold_kanban/engine'
2
+ require 'active_scaffold_kanban/version'
3
+ require 'active_scaffold_sortable' # ensure dependent gem is loaded
4
+
5
+ module ActiveScaffold
6
+ module Actions
7
+ ActiveScaffold.autoload_subdir('actions', self, File.dirname(__FILE__))
8
+ end
9
+
10
+ module Config
11
+ ActiveScaffold.autoload_subdir('config', self, File.dirname(__FILE__))
12
+ end
13
+
14
+ module Helpers
15
+ ActiveScaffold.autoload_subdir('helpers', self, File.dirname(__FILE__))
16
+ end
17
+ end
18
+
19
+ ActiveScaffold.stylesheets << 'active_scaffold_kanban'
20
+ ActiveScaffold.javascripts << 'active_scaffold_kanban'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_scaffold_kanban
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Cambra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_scaffold
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.7.10
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.7.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_scaffold_sortable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.2
41
+ description: User may reorder records and change to a different column
42
+ email: activescaffold@googlegroups.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files:
46
+ - README.md
47
+ files:
48
+ - LICENSE
49
+ - README.md
50
+ - app/assets/javascripts/active_scaffold_kanban.js
51
+ - app/assets/stylesheets/active_scaffold_kanban.scss
52
+ - app/views/active_scaffold_overrides/_kanban.html.erb
53
+ - app/views/active_scaffold_overrides/_kanban_card.html.erb
54
+ - app/views/active_scaffold_overrides/_list.html.erb
55
+ - app/views/active_scaffold_overrides/_new_record.js.erb
56
+ - app/views/active_scaffold_overrides/update_column.js.erb
57
+ - lib/active_scaffold/actions/kanban.rb
58
+ - lib/active_scaffold/config/kanban.rb
59
+ - lib/active_scaffold/helpers/kanban_helpers.rb
60
+ - lib/active_scaffold_kanban.rb
61
+ - lib/active_scaffold_kanban/engine.rb
62
+ - lib/active_scaffold_kanban/version.rb
63
+ homepage: https://activescaffold.eu
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.5.11
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Show records as kanban board, using ActiveScaffold
86
+ test_files: []