Skip to content

Commit

Permalink
added solution picker to initiative creation screen
Browse files Browse the repository at this point in the history
  • Loading branch information
dereke committed Oct 23, 2019
1 parent b49beaf commit 9981308
Show file tree
Hide file tree
Showing 43 changed files with 878 additions and 171 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"rules": {},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"browser": true,
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/group_types_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def create
@group_type = GroupType.new(group_type_params)

if @group_type.save
redirect_to group_types_path, notice: 'Group type was successfully created.'
redirect_to group_types_path,
notice: 'Group type was successfully created.'
else
render :new
end
Expand All @@ -35,7 +36,8 @@ def update

def destroy
@group_type.destroy!
redirect_to group_types_url, notice: 'Group type was successfully destroyed.'
redirect_to group_types_url,
notice: 'Group type was successfully destroyed.'
end

private
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/group_websites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ def create
@group_website = @group.websites.new(group_website_params)

if @group_website.save
redirect_to group_group_websites_url(@group), notice: 'Group website was successfully created.'
redirect_to group_group_websites_url(@group),
notice: 'Group website was successfully created.'
else
render :new
end
end

def update
if @group_website.update(group_website_params)
redirect_to [@group, @group_website], notice: 'Group website was successfully updated.'
redirect_to [@group, @group_website],
notice: 'Group website was successfully updated.'
else
render :edit
end
end

def destroy
@group_website.destroy
redirect_to group_group_websites_url(@group), notice: 'Group website was successfully destroyed.'
redirect_to group_group_websites_url(@group),
notice: 'Group website was successfully destroyed.'
end

private
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/initiative_statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ def create
@initiative_status = InitiativeStatus.new(initiative_status_params)

if @initiative_status.save
redirect_to initiative_statuses_path, notice: 'Initiative status was successfully created.'
redirect_to initiative_statuses_path,
notice: 'Initiative status was successfully created.'
else
render :new
end
end

def update
if @initiative_status.update(initiative_status_params)
redirect_to @initiative_status, notice: 'Initiative status was successfully updated.'
redirect_to @initiative_status,
notice: 'Initiative status was successfully updated.'
else
render :edit
end
end

def destroy
@initiative_status.destroy
redirect_to initiative_statuses_url, notice: 'Initiative status was successfully destroyed.'
redirect_to initiative_statuses_url,
notice: 'Initiative status was successfully destroyed.'
end

private
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/initiatives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def set_edit_data
InitiativeStatus.all.map do |initiative_status|
[initiative_status.name, initiative_status.id]
end

@taxonomy_hierarchy_json = Solution.hierarchy.to_json
end

# rubocop:disable Metrics/MethodLength
Expand All @@ -75,7 +77,8 @@ def initiative_params
:partner_groups_role,
:status_id,
:gdpr,
:gdpr_email_verified
:gdpr_email_verified,
solutions_attributes: %i[solution_id solution_class_id]
)
end
# rubocop:enable Metrics/MethodLength
Expand Down
2 changes: 2 additions & 0 deletions app/models/initiative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Initiative < ApplicationRecord
delegate :name, prefix: true, to: :status
delegate :name, prefix: true, to: :lead_group
after_initialize :set_default_location
has_many :solutions, class_name: 'InitiativeSolution', dependent: :destroy
accepts_nested_attributes_for :solutions

def set_default_location
self.latitude ||= 51.742
Expand Down
7 changes: 7 additions & 0 deletions app/models/initiative_solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class InitiativeSolution < ApplicationRecord
belongs_to :solution
belongs_to :solution_class
belongs_to :initiative
end
3 changes: 1 addition & 2 deletions app/models/initiative_status.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true

class InitiativeStatus < ApplicationRecord
end
class InitiativeStatus < ApplicationRecord; end
59 changes: 58 additions & 1 deletion app/models/solution.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
# frozen_string_literal: true

class Solution < ApplicationRecord
# has_many :solution_classes
has_many :solution_solution_classes, dependent: :destroy

def self.deep_load_solutions
Solution.all.includes(
solution_solution_classes: { solution_class: { theme: :sector } }
)
end

def self.hierarchy
hierarchy = []
deep_load_solutions.find_each do |s|
s.solution_solution_classes.each do |sc|
sector = populate_sector(hierarchy, sc.solution_class)
theme = populate_theme(sector, sc.solution_class)
solution_class = populate_solution_class(theme, sc.solution_class.name)
populate_solution(solution_class, sc.solution, sc.solution_class)
end
end
hierarchy
end

def self.populate_sector(hierarchy, solution_class)
name = solution_class.theme.sector.name
sector = hierarchy.select { |s| s[:name] == name }.first
if sector.nil?
sector = { name: name, themes: [] }
hierarchy << sector
end
sector
end

def self.populate_theme(sector, solution_class)
name = solution_class.theme_name
themes = sector[:themes]
theme = themes.select { |t| t[:name] == name }.first
if theme.nil?
theme = { name: name, classes: [] }
sector[:themes] << theme
end
theme
end

def self.populate_solution_class(theme, name)
solution_class = theme[:classes].select { |t| t[:name] == name }.first
if solution_class.nil?
solution_class = { name: name, solutions: [] }
theme[:classes] << solution_class
end
solution_class
end

def self.populate_solution(mapped_solution_class, solution, solution_class)
name = solution.name
solution = {
name: name, solution_class_id: solution_class.id, solution_id: solution.id
}
mapped_solution_class[:solutions] << solution
end
end
1 change: 1 addition & 0 deletions app/models/solution_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

class SolutionClass < ApplicationRecord
belongs_to :theme
delegate :name, to: :theme, prefix: true
end
3 changes: 1 addition & 2 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true

class Tag < ApplicationRecord
end
class Tag < ApplicationRecord; end
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ class User < ApplicationRecord
:recoverable,
:rememberable,
:validatable
has_many :groups, dependent: :nullify, foreign_key: 'owner_id', inverse_of: :owner
has_many :groups,
dependent: :nullify, foreign_key: 'owner_id', inverse_of: :owner
end
19 changes: 14 additions & 5 deletions app/views/initiatives/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% content_for :page_javascript do %>
<%= javascript_packs_with_chunks_tag 'initiative_location_picker', 'data-turbolinks-track': 'reload' %>
<%= javascript_packs_with_chunks_tag 'initiative_solution_picker', 'data-turbolinks-track': 'reload' %>
<script type="text/json" id="taxonomy_hierarchy_json"><%=taxonomy_hierarchy_json.html_safe%></script>
<% end %>

<%= form_with(model: initiative, local: true, html: {class: 'FormField-form'}) do |f| %>
Expand All @@ -20,11 +22,6 @@
<%= f.text_field :name, autofocus: true %>
<% end %>

<%= f.form_field :alternative_solution_name do %>
<%= f.label :alternative_solution_name %>
<%= f.text_field :alternative_solution_name %>
<% end %>

<%= f.form_field :summary do %>
<%= f.label :summary %>
<%= f.text_field :summary %>
Expand Down Expand Up @@ -92,6 +89,18 @@
<%= f.select :status_id, initiative_statuses %>
<% end %>

<h2>Solutions</h2>

<%= f.form_field :choose_solution do %>
<%= f.label :choose_solution %>
<div id="solution_picker"></div>
<% end %>

<%= f.form_field :alternative_solution_name do %>
<%= f.label :alternative_solution_name %>
<%= f.text_field :alternative_solution_name %>
<% end %>

<%= f.form_field :gdpr do %>
<%= f.label :gdpr %>
<%= f.check_box :gdpr %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/initiatives/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Editing Initiative</h1>

<%= render 'form', initiative: @initiative, groups: @groups, initiative_statuses: @initiative_statuses %>
<%= render 'form', initiative: @initiative, groups: @groups, initiative_statuses: @initiative_statuses, taxonomy_hierarchy_json: @taxonomy_hierarchy_json %>

<%= link_to 'Show', @initiative %> |
<%= link_to 'Back', initiatives_path %>
2 changes: 1 addition & 1 deletion app/views/initiatives/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>New Initiative</h1>

<%= render 'form', initiative: @initiative, groups: @groups, initiative_statuses: @initiative_statuses %>
<%= render 'form', initiative: @initiative, groups: @groups, initiative_statuses: @initiative_statuses, taxonomy_hierarchy_json: @taxonomy_hierarchy_json %>

<%= link_to 'Back', initiatives_path %>
4 changes: 3 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ module.exports = function(api) {
modules: false,
exclude: ["transform-typeof-symbol"]
}
]
],
"babel-preset-hyperdom"
].filter(Boolean),

plugins: [
require("babel-plugin-macros"),
require("@babel/plugin-syntax-dynamic-import").default,
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20191022214904_create_initiative_solutions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateInitiativeSolutions < ActiveRecord::Migration[6.0]
def change
create_table :initiative_solutions do |t|
t.references :solution, null: false, foreign_key: true
t.references :solution_class, null: false, foreign_key: true
t.references :initiative, null: false, foreign_key: true

t.timestamps
end
end
end
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_10_21_195951) do
ActiveRecord::Schema.define(version: 2019_10_22_214904) do
# These are extensions that must be enabled in order to support this database
enable_extension 'plpgsql'

Expand Down Expand Up @@ -52,6 +52,19 @@
t.index %w[owner_id], name: 'index_groups_on_owner_id'
end

create_table 'initiative_solutions', force: :cascade do |t|
t.bigint 'solution_id', null: false
t.bigint 'solution_class_id', null: false
t.bigint 'initiative_id', null: false
t.datetime 'created_at', precision: 6, null: false
t.datetime 'updated_at', precision: 6, null: false
t.index %w[initiative_id],
name: 'index_initiative_solutions_on_initiative_id'
t.index %w[solution_class_id],
name: 'index_initiative_solutions_on_solution_class_id'
t.index %w[solution_id], name: 'index_initiative_solutions_on_solution_id'
end

create_table 'initiative_statuses', force: :cascade do |t|
t.string 'name'
t.string 'description'
Expand Down Expand Up @@ -144,6 +157,9 @@
add_foreign_key 'group_group_types', 'group_types'
add_foreign_key 'group_group_types', 'groups'
add_foreign_key 'group_websites', 'groups'
add_foreign_key 'initiative_solutions', 'initiatives'
add_foreign_key 'initiative_solutions', 'solution_classes'
add_foreign_key 'initiative_solutions', 'solutions'
add_foreign_key 'initiatives', 'groups', column: 'lead_group_id'
add_foreign_key 'initiatives', 'initiative_statuses', column: 'status_id'
add_foreign_key 'solution_classes', 'themes'
Expand Down
Loading

0 comments on commit 9981308

Please sign in to comment.