@@ -29,43 +29,61 @@ def index
2929 # rubocop:enable Metrics/AbcSize
3030
3131 # GET /plans/new
32- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
32+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
3333 def new
3434 @plan = Plan . new
3535 authorize @plan
36-
37- # get funder templates
38- funder_templates = Template . published
39- . joins ( :org )
40- . merge ( Org . funder )
41- . distinct
36+ @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 ( &: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+ # get templates of user's customised org
48+ user_org_custom_templates = Template . latest_customizable . where ( family_id : customization_ids )
49+ . uniq . sort_by ( &:title )
50+
51+ # get funder templates no customised templates
52+ funder_non_customised_templates = Template . published
53+ . joins ( :org )
54+ . where ( orgs : { org_type : Org . org_type_values_for ( :funder ) } )
55+ # The next line removes templates that belong to a family that
56+ # has customised templates
57+ . where . not ( family_id : customization_ids )
58+ . uniq . sort_by ( &:title )
4259
4360 # get global templates
4461 global_templates = Template . published
4562 . where ( is_default : true )
46- . distinct
47-
48- # get templates of user's org
49- user_org_templates = Template . published
50- . where ( org : current_user . org )
51- . distinct
63+ # The next line removes templates that belong to a family that
64+ # has customised templates
65+ . where . not ( family_id : customization_ids )
66+ . uniq . sort_by ( &:title )
5267
5368 # create templates-grouped hash
5469 @templates_grouped = {
55- _ ( "Your Organisation's Templates:" ) => user_org_templates . map do |t |
70+ _ ( "Your Organisation's Templates:" ) => user_org_own_templates . map do |t |
71+ [ t . title , t . id ]
72+ end ,
73+ _ ( "Your Organisation's Customised Templates:" ) => user_org_custom_templates . map do |t |
5674 [ t . title , t . id ]
5775 end ,
5876 _ ( 'Global Templates:' ) => global_templates . map do |t |
5977 [ t . title , t . id ]
6078 end ,
61- _ ( 'Funder Templates:' ) => funder_templates . map do |t |
79+ _ ( 'Funder Templates:' ) => funder_non_customised_templates . map do |t |
6280 [ t . title , t . id ]
6381 end
6482 } . reject { |_ , val | val . empty? }
6583
6684 respond_to :html
6785 end
68- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
86+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6987
7088 # POST /plans
7189 # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
0 commit comments