@@ -33,24 +33,57 @@ def index
3333 def new
3434 @plan = Plan . new
3535 authorize @plan
36-
37- # Get all of the available funders and non-funder orgs
38- @funders = Org . funder
39- . includes ( identifiers : :identifier_scheme )
40- . joins ( :templates )
41- . where ( templates : { published : true } ) . uniq . sort_by ( &:name )
42- @orgs = ( Org . includes ( identifiers : :identifier_scheme ) . organisation +
43- Org . includes ( identifiers : :identifier_scheme ) . institution +
44- Org . includes ( identifiers : :identifier_scheme ) . default_orgs )
45- @orgs = @orgs . flatten . uniq . sort_by ( &:name )
46-
4736 @plan . org_id = current_user . org &.id
37+ # looks into the list of families of templates
38+ customizations = Template . latest_customized_version_per_org ( @plan . org_id )
39+ customization_ids = customizations . select { |t | t . published? } . collect ( &:customization_of )
40+
41+ # get templates of user's own org
42+ user_org_own_templates = Template . organisationally_visible
43+ . where ( org_id : @plan . org_id , customization_of : nil )
44+ . published
45+ . uniq . sort_by ( &:title )
46+
47+
48+ # get templates of user's customised org
49+ user_org_custom_templates = Template . latest_customizable . where ( family_id : customization_ids )
50+ . uniq . sort_by ( &:title )
51+
52+
53+ # get funder templates no customised templates
54+ funder_non_customised_templates = Template . published
55+ . joins ( :org )
56+ . where ( orgs : { org_type : Org . org_type_values_for ( :funder ) } )
57+ # The next line removes templates that belong to a family that
58+ # has customised templates
59+ . where . not ( family_id : customization_ids )
60+ . uniq . sort_by ( &:title )
61+
62+ # get global templates
63+ global_templates = Template . published
64+ . where ( is_default : true )
65+ # The next line removes templates that belong to a family that
66+ # has customised templates
67+ . where . not ( family_id : customization_ids )
68+ . uniq . sort_by ( &:title )
69+
70+
71+ # create templates-grouped hash
72+ @templates_grouped = {
73+ _ ( "Your Organisation's Templates:" ) => user_org_own_templates . map {
74+ |t | [ t . title , t . id ]
75+ } ,
76+ _ ( "Your Organisation's Customised Templates:" ) => user_org_custom_templates . map {
77+ |t | [ t . title , t . id ]
78+ } ,
79+ _ ( "Global Templates:" ) => global_templates . map {
80+ |t | [ t . title , t . id ]
81+ } ,
82+ _ ( "Funder Templates:" ) => funder_non_customised_templates . map {
83+ |t | [ t . title , t . id ]
84+ }
85+ } . reject { |_ , val | val . empty? }
4886
49- # TODO: is this still used? We cannot switch this to use the :plan_params
50- # strong params because any calls that do not include `plan` in the
51- # query string will fail
52- flash [ :notice ] = "#{ _ ( 'This is a' ) } <strong>#{ _ ( 'test plan' ) } </strong>" if params . key? ( :test )
53- @is_test = params [ :test ] ||= false
5487 respond_to :html
5588 end
5689 # rubocop:enable Metrics/AbcSize
0 commit comments