Skip to content

Commit

Permalink
Engine CI setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Blat committed Jun 10, 2019
1 parent 35f88b5 commit fcc4f11
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ app/javascript/gencat
app/javascript/packs/gencat.js
app/javascript/valencia
app/javascript/packs/valencia.js
app/javascript/custom_fields_data_grid_plugin
app/javascript/packs/custom_fields_data_grid_plugin.js

1 change: 0 additions & 1 deletion app/javascript/custom_fields_data_grid_plugin

This file was deleted.

1 change: 0 additions & 1 deletion app/javascript/packs/custom_fields_data_grid_plugin.js

This file was deleted.

8 changes: 8 additions & 0 deletions script/custom_engines_ci_setup
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ cd ~/$CUSTOM_ENGINE_NAME_1
git checkout $branch
./script/setup.sh
./script/create_webpacker_symlinks.sh

# Plugins
for custom_fields_dir in $(ls -d ~/gobierto/vendor/gobierto_engines/custom-fields*); do
cd $custom_fields_dir
./script/setup.sh
./script/create_webpacker_symlinks.sh
cd ..
done
50 changes: 50 additions & 0 deletions vendor/gobierto_engines/custom-fields-data-grid-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Custom Fields Plugins Engine

The purpose of this engine is to define new plugins or override existing ones, used by custom fields of this type.

## Usage


* Declare the available plugins in the railtie adding their names to the list of `custom_field_plugins`, for example, a `test` type:

```ruby
Rails.application.config.tap do |conf|
conf.custom_field_plugins += ["test"]
end
```

The `test` type will be available in the options of plugin custom field type

* Define the translations of the `test` plugin type in `config/locales` for `gobierto_admin.gobierto_common.custom_fields.plugin.test`

* In the admin form of an instance with custom field recors of `test` type a `form()` javascript method will be expected with the name:

```
window.GobiertoAdmin.gobierto_common_custom_field_records_test_plugin_controller.form({ uid: record_uid })
```

The argument received by this method will be an object containing at least the uid of the custom field record.

The payload of the field can be accessed and changed in the hidden field:

```
<input type="hidden" name="instance_name[custom_records][record_uid][value]" id="instance_name_custom_records_record_uid_value" value="..." />
```

`instance_name` changes depending on the item the record belongs to (project, citizen_charter, person...) and the record_uid is given by the record

The javascript is responsible to read, manipulate and change the value. When the form is sent, the value of the hidden field will be saved in the corresponding record.

## Installation
Previously you have to set `DEV_DIR` environment variable. Local gobierto application must be
under this path, i.e. the gobierto path must be `$DEV_DIR/gobierto`

Clone this repo and run `script/setup.sh`. It will create the following symbolic link in local
gobierto path: `$DEV_DIR/gobierto/vendor/gobierto_engines/custom-field-plugins-engine`.

## Contributing
Contribution directions go here.

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './modules/data_grid_plugin.js'
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { Grid, Data, Formatters, Editors, Plugins } from 'slickgrid-es6';

window.GobiertoAdmin.GobiertoCommonCustomFieldRecordsDataGridPluginController = (function() {

function GobiertoCommonCustomFieldRecordsDataGridPluginController() {}

GobiertoCommonCustomFieldRecordsDataGridPluginController.prototype.form = function(opts = {}) {
_handlePluginData(opts.uid);
};

function _handlePluginData(uid) {
let element = $(`[data-uid=${uid}]`)
let id = element.attr('id')
let data = JSON.parse($(`#${id}`).find("input[name$='[value]'").val())

_applyPluginStyles(element)
_slickGrid(id, data)

$("form").submit(
function() {
$(".v_container .v_el .form_item.plugin_field.data_grid").each(function(i) {
let uid = $(this).data("uid")
$(`input[name$='[${uid}][value]']`).val(JSON.stringify($(this).data("slickGrid").getData()));
});
}
);
}

function _applyPluginStyles(element) {
element.wrap("<div class='v_container'><div class='v_el v_el_level v_el_full_content'></div></div>");
element.find("div.custom_field_value").addClass("indicators_table")
element.find("div.data-container").addClass("slickgrid-container").css({ width: "100%", height: "500px" });
}

function _slickGrid(id, data) {
function requiredFieldValidator(value) {
if (value == null || value == undefined || !value.length) {
return {valid: false, msg: "This is a required field"};
} else {
return {valid: true, msg: null};
}
}

function _headerDateName(startYear, startMonth, offset) {
return `${startYear + Math.floor((startMonth - 1 + offset)/12)}-${((startMonth - 1 + offset) % 12 + 1).toString().padStart(2, '0')}`;
}

function _dateColumn(startYear, startMonth, offset) {
let headerName = _headerDateName(startYear, startMonth, offset);
return {id: headerName, name: headerName, field: headerName, width: 100, editor: Editors.Float};
}

function _dateColumnsFromData(data) {
let columns = [];
let firstRow = Object.assign({}, data[0]);
delete firstRow.indicator
let rowKeys = Object.keys(firstRow).sort();
let lastDateValues = rowKeys[rowKeys.length - 1].split("-");
for (let i = 0; i < rowKeys.length; i++) {
let dateValues = rowKeys[i].split("-");
columns.push(_dateColumn(parseInt(dateValues[0]), parseInt(dateValues[1]), 0));
}
return {
lastYear: parseInt(lastDateValues[0]),
lastMonth: parseInt(lastDateValues[1]),
columns: columns
};
}

let new_column_id = `new_column_${id}`
let dateColumnsCount = 5;
let startYear = 2018;
let startMonth = 12;
let grid;
let columns = [{
id: "indicator",
name: "",
field: "indicator",
width: 120,
cssClass: "cell-title",
editor: Editors.Text,
validator: requiredFieldValidator
}];

let options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: false,
enableColumnReorder: false,
autoEdit: true,
itemsCountId: `${id}_items`
};

$(function () {
if (data.length === 0) {
// initialize an empty table
for (let i = 0; i < 5; i++) {
let d = (data[i] = {});

d["indicator"] = "indicator " + i;
for (let o = 0; o < dateColumnsCount; o++) {
d[_headerDateName(startYear, startMonth, o)] = null;
}
}

for (let i = 0; i < dateColumnsCount; i++) {
columns.push(_dateColumn(startYear, startMonth, i));
}
} else {
let dateColumns = _dateColumnsFromData(data);
columns = columns.concat(dateColumns.columns);
startYear = dateColumns.lastYear;
startMonth = dateColumns.lastMonth;
dateColumnsCount = 1;
}

columns.push({
id: new_column_id,
name: "Add",
field: new_column_id,
witdh: 50,
headerCssClass: `add-${new_column_id}`,
sortable: false,
headerDateNameFunction: _headerDateName,
startYear: startYear,
startMonth: startMonth,
offset: dateColumnsCount});

grid = new Grid(`#${id} .data-container`, data, columns, options);
$(`#${id}`).data('slickGrid', grid);

grid.setSelectionModel(new Plugins.CellSelectionModel());

grid.onHeaderClick.subscribe(function (e, args) {
let item = args.column;
if(item.name === "Add"){
let columns = args.grid.getColumns();
let lastColumn = columns.pop();
let lastYear = lastColumn.headerDateNameFunction(lastColumn.startYear, lastColumn.startMonth, lastColumn.offset)
let columnDefinition = {id: lastYear, name: lastYear, field: lastYear, width: 100, editor: Editors.Integer};
lastColumn.offset++;
columns.push(columnDefinition);
columns.push(lastColumn);
args.grid.setColumns(columns);
args.grid.render();
}
});

grid.onAddNewRow.subscribe(function (e, args) {
let item = args.item;
grid.invalidateRow(data.length);
data.push(item);
grid.updateRowCount();
grid.invalidate();
grid.render();
$(`#${grid.getOptions().itemsCountId}`).html(data.length);
});
})

}
return GobiertoCommonCustomFieldRecordsDataGridPluginController;
})();

window.GobiertoAdmin.gobierto_common_custom_field_records_data_grid_plugin_controller = new GobiertoAdmin.GobiertoCommonCustomFieldRecordsDataGridPluginController;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

import 'custom_fields_data_grid_plugin'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
base_locale: es
locales: [en, es, ca]

data:
read:
- config/locales/**/%{locale}.yml
- config/locales/**/*/%{locale}.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
ca:
gobierto_admin:
gobierto_common:
custom_fields:
custom_fields:
plugin:
data_grid: Taula de dades
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
en:
gobierto_admin:
gobierto_common:
custom_fields:
custom_fields:
plugin:
data_grid: Data grid
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
es:
gobierto_admin:
gobierto_common:
custom_fields:
custom_fields:
plugin:
data_grid: Tabla de datos
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

begin
require 'rails/railtie'
rescue LoadError
else
class CustomFieldsDataGridPlugin
class Railtie < Rails::Railtie
base_path = File.join(File.dirname(__FILE__), "../..")
Rails.application.config.tap do |conf|
conf.custom_field_plugins += ["data_grid"]
conf.i18n.load_path += Dir[File.join(base_path, 'config', 'locales', '**', '*.{rb,yml}')]
end
Webpacker::Compiler.watched_paths << "app/javascript/plugin/**/*.js"
Webpacker::Compiler.watched_paths << "app/javascript/packs/*.js"
end
end
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "custom_fields_data_grid_plugin/railtie"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

engine_webpack_entry_path="app/javascripts/packs/custom_fields_data_grid_plugin.js"
engine_webpack_source_path="app/javascripts/custom_fields_data_grid_plugin"

echo "Running webpacker setup"

while getopts ":d:" opt; do
case $opt in
d) opt_dir=$OPTARG ;;
esac
done
gobierto_dir=${opt_dir:-"$DEV_DIR/gobierto"}

if [ -z "$gobierto_dir" ]
then
echo "Please set DEV_DIR in your .bash_profile before running this script or invoke it with -d gobierto_dir, where gobierto_dir is the path of gobierto release";
else
source_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )

# This configuration is taken from gobierto/config/webpacker.yml
gobierto_webpack_source_path="$gobierto_dir/app/javascript"
gobierto_webpack_entry_path="$gobierto_webpack_source_path/packs"

echo " Creating webpacker symlinks..."

echo " executing ln -s $source_path/$engine_webpack_entry_path $gobierto_webpack_entry_path"
ln -s $source_path/$engine_webpack_entry_path $gobierto_webpack_entry_path

echo " executing ln -s $source_path/$engine_webpack_source_path $gobierto_webpack_source_path"
ln -s $source_path/$engine_webpack_source_path $gobierto_webpack_source_path

echo " [OK]"
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

engine_name="custom-field-plugins-engine"

echo "Running setup script for $engine_name"
while getopts ":d:" opt; do
case $opt in
d) opt_dir=$OPTARG ;;
esac
done

dev_dir=${opt_dir:-$DEV_DIR}

if [ -z "$dev_dir" ]
then
echo "Please set DEV_DIR in your .bash_profile before running this script or invoke it with -d dev_dir, where dev_dir is the path containing gobierto";
else
engines_path=${GOBIERTO_ENGINES_PATH:-"$DEV_DIR/gobierto/vendor/gobierto_engines"}
source_path=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
echo "Using dev_dir: $dev_dir"
echo "Using engines_path: $engines_path"

echo "Creating symlinks..."
echo "executing ln -s $source_path $engines_path"
ln -F -s $source_path $engines_path

$source_path/script/create_webpacker_symlinks.sh -d $dev_dir/gobierto

echo "executing ln -s $dev_dir/gobierto/node_modules $source_path"
ln -F -s $dev_dir/gobierto/node_modules $source_path

echo "[OK]"
fi

0 comments on commit fcc4f11

Please sign in to comment.