@@ -88,16 +88,19 @@ def self.experimental_styles_for(page_type)
8888 end
8989 end
9090
91- def self . include_all_fields_in_category ( user , page , category_label )
92- category = AttributeCategory . where (
91+ def self . include_all_fields_in_category ( user , page , category_labels )
92+ category_labels = Array ( category_labels )
93+
94+ # Find all categories matching ANY of the provided labels (case-insensitive)
95+ # This allows users with renamed categories to still match
96+ categories = AttributeCategory . where (
9397 user_id : user . id ,
94- entity_type : page . page_type . downcase ,
95- label : category_label
96- )
97- return nil if category . empty?
98+ entity_type : page . page_type . downcase
99+ ) . where ( 'LOWER(label) IN (?)' , category_labels . map ( &:downcase ) )
100+ return nil if categories . empty?
98101
99102 fields = AttributeField . where (
100- attribute_category_id : category . pluck ( :id ) ,
103+ attribute_category_id : categories . pluck ( :id ) ,
101104 field_type : [ 'name' , 'text_area' , 'textarea' ] ,
102105 hidden : [ nil , false ]
103106 )
@@ -118,19 +121,21 @@ def self.include_all_fields_in_category(user, page, category_label)
118121 end
119122 end
120123
121- def self . include_specific_field ( user , page , category_label , field_label )
122- category = AttributeCategory . find_by (
123- user_id : user . id ,
124- entity_type : page . page_type . downcase ,
125- label : category_label
126- )
127- return nil if category . nil?
124+ def self . include_specific_field ( user , page , _category_label , field_label )
125+ # Note: category_label parameter kept for backward compatibility but ignored
126+ # We now search by field label across ALL categories for this user/page_type
127+ # This allows users with custom/renamed categories to still match fields by label
128128
129- field = AttributeField . find_by (
130- attribute_category_id : category . id ,
131- label : field_label ,
132- hidden : [ nil , false ]
133- )
129+ field = AttributeField . joins ( :attribute_category )
130+ . where (
131+ attribute_categories : {
132+ user_id : user . id ,
133+ entity_type : page . page_type . downcase
134+ } ,
135+ label : field_label ,
136+ hidden : [ nil , false ]
137+ )
138+ . first
134139 return nil if field . nil?
135140
136141 value = Attribute . where ( attribute_field_id : field . id ,
@@ -144,4 +149,44 @@ def self.include_specific_field(user, page, category_label, field_label)
144149 # in the form of [field, value].
145150 return [ field , value . value ]
146151 end
152+
153+ def self . get_additional_filled_fields ( user , page , exclude_field_ids : [ ] )
154+ # Get all text-type fields for this page type that have values
155+ # but weren't already included in the relevant_fields
156+
157+ category_ids = AttributeCategory . where (
158+ user_id : user . id ,
159+ entity_type : page . page_type . downcase ,
160+ hidden : [ nil , false ]
161+ ) . where . not ( label : [ 'Settings' , 'Contributors' , 'Gallery' , 'Changelog' ] )
162+ . pluck ( :id )
163+
164+ return [ ] if category_ids . empty?
165+
166+ fields = AttributeField . where (
167+ attribute_category_id : category_ids ,
168+ field_type : [ 'name' , 'text_area' , 'textarea' ] ,
169+ hidden : [ nil , false ]
170+ ) . where . not ( id : exclude_field_ids )
171+ . includes ( :attribute_category )
172+
173+ return [ ] if fields . empty?
174+
175+ answers = Attribute . where (
176+ attribute_field_id : fields . pluck ( :id ) ,
177+ entity_id : page . id ,
178+ entity_type : page . page_type
179+ ) . where . not ( value : IGNORED_VALUES )
180+
181+ return [ ] if answers . empty?
182+
183+ # Sort by category position, then field position
184+ answers . map do |answer |
185+ field = fields . find { |f | f . id == answer . attribute_field_id }
186+ next nil if field . nil?
187+ [ field , answer . value ]
188+ end . compact . sort_by do |field , _ |
189+ [ field . attribute_category &.position || 999 , field . position || 999 ]
190+ end
191+ end
147192end
0 commit comments