Module: Labimotion::ElementHelpers
- Extended by:
- Grape::API::Helpers
- Defined in:
- lib/labimotion/helpers/element_helpers.rb
Overview
ElementHelpers
Instance Method Summary collapse
- #attach_thumbnail(_attachments) ⇒ Object
- #create_element(current_user, params) ⇒ Object
- #create_element_klass(current_user, params) ⇒ Object
- #create_repo_klass(params, current_user, origin) ⇒ Object
- #element_revisions(params) ⇒ Object
- #klass_list(is_generic_only) ⇒ Object
- #list_serialized_elements(params, current_user) ⇒ Object
- #list_user_elements(scope, params) ⇒ Object
- #split_elements(ui_state, current_user) ⇒ Object
- #update_element_by_id(current_user, params) ⇒ Object
- #update_element_klass(current_user, params) ⇒ Object
- #upload_generics_files(current_user, params) ⇒ Object
- #validate_klass(attributes) ⇒ Object
Instance Method Details
#attach_thumbnail(_attachments) ⇒ Object
347 348 349 350 351 352 353 354 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 347 def attach_thumbnail() = &.map do || _att = Entities::AttachmentEntity.represent(, serializable: true) _att[:thumbnail] = .thumb ? Base64.encode64(.read_thumbnail) : nil _att end end |
#create_element(current_user, params) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 66 def create_element(current_user, params) klass = params[:element_klass] || {} uuid = SecureRandom.uuid params[:properties]['uuid'] = uuid params[:properties]['klass_uuid'] = klass[:uuid] params[:properties]['pkg'] = Labimotion::Utils.pkg(params[:properties]['pkg']) params[:properties]['klass'] = 'Element' params[:properties]['identifier'] = klass[:identifier] properties = params[:properties] properties.delete('flow') unless properties['flow'].nil? properties.delete('flowObject') unless properties['flowObject'].nil? properties.delete('select_options') unless properties['select_options'].nil? attributes = { name: params[:name], element_klass_id: klass[:id], uuid: uuid, klass_uuid: klass[:uuid], properties: properties, properties_release: params[:properties_release], created_by: current_user.id, } element = Labimotion::Element.new(attributes) if params[:collection_id] collection = current_user.collections.find(params[:collection_id]) element.collections << collection end all_coll = Collection.get_all_collection_for_user(current_user.id) element.collections << all_coll element.save! element.properties = update_sample_association(params[:properties], current_user, element) element.container = update_datamodel(params[:container], current_user) element.save! update_element_labels(element, params[:user_labels], current_user.id) element.save_segments(segments: params[:segments], current_user_id: current_user.id) element rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#create_element_klass(current_user, params) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 20 def create_element_klass(current_user, params) uuid = SecureRandom.uuid template = { uuid: uuid, layers: {}, select_options: {} } attributes = declared(params, include_missing: false) attributes[:properties_template] = template if attributes[:properties_template].nil? attributes[:properties_template]['uuid'] = uuid attributes[:properties_template]['pkg'] = Labimotion::Utils.pkg(attributes[:properties_template]['pkg']) attributes[:properties_template]['klass'] = 'ElementKlass' attributes[:is_active] = false attributes[:uuid] = uuid attributes[:released_at] = DateTime.now attributes[:properties_release] = attributes[:properties_template] attributes[:created_by] = current_user.id new_klass = Labimotion::ElementKlass.create!(attributes) new_klass.reload new_klass.create_klasses_revision(current_user) klass_names_file = Labimotion::KLASSES_JSON # Rails.root.join('app/packs/klasses.json') klasses = Labimotion::ElementKlass.where(is_active: true)&.pluck(:name) || [] File.write(klass_names_file, klasses) klasses rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#create_repo_klass(params, current_user, origin) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 330 def create_repo_klass(params, current_user, origin) response = Labimotion::TemplateHub.fetch_identifier('ElementKlass', params[:identifier], origin) attributes = response.slice('name', 'label', 'desc', 'icon_name', 'uuid', 'klass_prefix', 'is_generic', 'identifier', 'properties_release', 'version') attributes['properties_release']['identifier'] = attributes['identifier'] attributes['properties_template'] = attributes['properties_release'] attributes['place'] = ((Labimotion::ElementKlass.all.length * 10) || 0) + 10 attributes['is_active'] = false attributes['updated_by'] = current_user.id attributes['sync_by'] = current_user.id attributes['sync_time'] = DateTime.now validate_klass(attributes) rescue StandardError => e Labimotion.log_exception(e, current_user) # { error: e.message } raise e end |
#element_revisions(params) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 190 def element_revisions(params) klass = Labimotion::Element.find(params[:id]) list = klass.elements_revisions unless klass.nil? list&.sort_by(&:created_at)&.reverse&.first(10) rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#klass_list(is_generic_only) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 12 def klass_list(is_generic_only) if is_generic_only == true Labimotion::ElementKlass.where(is_active: true, is_generic: true).order('place') || [] else Labimotion::ElementKlass.where(is_active: true).order('place') || [] end end |
#list_serialized_elements(params, current_user) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 239 def list_serialized_elements(params, current_user) collection_id = if params[:collection_id] Collection .belongs_to_or_shared_by(current_user.id, current_user.group_ids) .find_by(id: params[:collection_id])&.id elsif params[:sync_collection_id] current_user .all_sync_in_collections_users .find_by(id: params[:sync_collection_id])&.collection&.id end scope = if collection_id Labimotion::Element .joins(:element_klass, :collections_elements) .where( element_klasses: { name: params[:el_type] }, collections_elements: { collection_id: collection_id }, ).includes(:tag, collections: :sync_collections_users) else Labimotion::Element.none end ## TO DO: refactor labimotion from = params[:from_date] to = params[:to_date] by_created_at = params[:filter_created_at] || false if params[:sort_column]&.include?('.') layer, field = params[:sort_column].split('.') element_klass = Labimotion::ElementKlass.find_by(name: params[:el_type]) allowed_fields = element_klass.properties_release.dig(Labimotion::Prop::LAYERS, layer, Labimotion::Prop::FIELDS)&.pluck('field') || [] if field.in?(allowed_fields) query = ActiveRecord::Base.sanitize_sql( [ "LEFT JOIN LATERAL( SELECT field->'value' AS value FROM jsonb_array_elements(properties->'layers'->:layer->'fields') a(field) WHERE field->>'field' = :field ) a ON true", { layer: layer, field: field }, ], ) scope = scope.joins(query).order('value ASC NULLS FIRST') else scope = scope.order(updated_at: :desc) end else scope = scope.order(updated_at: :desc) end scope = scope.elements_created_time_from(Time.at(from)) if from && by_created_at scope = scope.elements_created_time_to(Time.at(to) + 1.day) if to && by_created_at scope = scope.elements_updated_time_from(Time.at(from)) if from && !by_created_at scope = scope.elements_updated_time_to(Time.at(to) + 1.day) if to && !by_created_at scope = scope.by_user_label(params[:user_label]) if params[:user_label] scope rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#list_user_elements(scope, params) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 199 def list_user_elements(scope, params) from = params[:from_date] to = params[:to_date] by_created_at = params[:filter_created_at] || false if params[:sort_column]&.include?('.') layer, field = params[:sort_column].split('.') element_klass = Labimotion::ElementKlass.find_by(name: params[:el_type]) allowed_fields = element_klass.properties_release.dig(Labimotion::Prop::LAYERS, layer, Labimotion::Prop::FIELDS)&.pluck('field') || [] if field.in?(allowed_fields) query = ActiveRecord::Base.sanitize_sql( [ "LEFT JOIN LATERAL( SELECT field->'value' AS value FROM jsonb_array_elements(properties->'layers'->:layer->'fields') a(field) WHERE field->>'field' = :field ) a ON true", { layer: layer, field: field }, ], ) scope = scope.joins(query).order('value ASC NULLS FIRST') else scope = scope.order(updated_at: :desc) end else scope = scope.order(updated_at: :desc) end scope = scope.created_time_from(Time.at(from)) if from && by_created_at scope = scope.created_time_to(Time.at(to) + 1.day) if to && by_created_at scope = scope.updated_time_from(Time.at(from)) if from && !by_created_at scope = scope.updated_time_to(Time.at(to) + 1.day) if to && !by_created_at scope rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#split_elements(ui_state, current_user) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 141 def split_elements(ui_state, current_user) col_id = ui_state[:currentCollectionId] || 0 element_ids = Labimotion::Element.for_user(current_user.id).for_ui_state_with_collection(ui_state[:element], Labimotion::CollectionsElement, col_id) klass_id = Labimotion::ElementKlass.find_by(name: ui_state[:element][:name])&.id Labimotion::Element.where(id: element_ids, element_klass_id: klass_id).each do |element| element.split(current_user, col_id) end {} end |
#update_element_by_id(current_user, params) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 107 def update_element_by_id(current_user, params) element = Labimotion::Element.find(params[:id]) update_datamodel(params[:container], current_user) properties = update_sample_association(params[:properties], current_user, element) params.delete(:container) params.delete(:properties) update_element_labels(element, params[:user_labels], current_user.id) params.delete(:user_labels) attributes = declared(params.except(:segments), include_missing: false) properties['pkg'] = Labimotion::Utils.pkg(properties['pkg']) if element.klass_uuid != properties['klass_uuid'] || element.properties != properties || element.name != params[:name] properties['klass'] = 'Element' uuid = SecureRandom.uuid properties['uuid'] = uuid properties.delete('flow') unless properties['flow'].nil? properties.delete('flowObject') unless properties['flowObject'].nil? properties.delete('select_options') unless properties['select_options'].nil? attributes['properties'] = properties attributes['properties']['uuid'] = uuid attributes['uuid'] = uuid attributes['klass_uuid'] = properties['klass_uuid'] element.update(attributes) end element.save_segments(segments: params[:segments], current_user_id: current_user.id) element rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#update_element_klass(current_user, params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 46 def update_element_klass(current_user, params) place = params[:place] || 100 begin place = place.to_i if place.present? && place.to_i == place.to_f rescue StandardError place = 100 end klass = Labimotion::ElementKlass.find(params[:id]) klass.label = params[:label] if params[:label].present? klass.klass_prefix = params[:klass_prefix] if params[:klass_prefix].present? klass.icon_name = params[:icon_name] if params[:icon_name].present? klass.desc = params[:desc] if params[:desc].present? klass.place = place klass.save! klass rescue StandardError => e Labimotion.log_exception(e, current_user) raise e end |
#upload_generics_files(current_user, params) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 151 def upload_generics_files(current_user, params) attach_ary = [] att_ary = create_uploads( Labimotion::Prop::ELEMENT, params[:att_id], params[:elfiles], params[:elInfo], current_user.id, ) if params[:elfiles].present? && params[:elInfo].present? (attach_ary << att_ary).flatten! unless att_ary&.empty? att_ary = create_uploads( Labimotion::Prop::SEGMENT, params[:att_id], params[:sefiles], params[:seInfo], current_user.id ) if params[:sefiles].present? && params[:seInfo].present? (attach_ary << att_ary).flatten! unless att_ary&.empty? if params[:attfiles].present? || params[:delfiles].present? then att_ary = ( params[:attfiles], params[:delfiles], "Labimotion::#{params[:att_type]}", params[:att_id], params[:attfilesIdentifier], current_user.id ) end (attach_ary << att_ary).flatten! unless att_ary&.empty? true rescue StandardError => e Labimotion.log_exception(e, current_user) false end |
#validate_klass(attributes) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 303 def validate_klass(attributes) element_klass = Labimotion::ElementKlass.find_by(identifier: attributes['identifier']) if element_klass.present? if element_klass['uuid'] == attributes['uuid'] && element_klass['version'] == attributes['version'] return { status: 'success', message: "This element: #{attributes['name']} has the latest version!" } else element_klass.update!(attributes) element_klass.create_klasses_revision(current_user) return { status: 'success', message: "This element: [#{attributes['name']}] has been upgraded to the version: #{attributes['version']}!" } end else exist_klass = Labimotion::ElementKlass.find_by(name: attributes['name']) if exist_klass.present? return { status: 'error', message: "The name [#{attributes['name']}] is already in use." } else attributes['created_by'] = current_user.id element_klass = Labimotion::ElementKlass.create!(attributes) element_klass.create_klasses_revision(current_user) return { status: 'success', message: "The element: #{attributes['name']} has been created using version: #{attributes['version']}!" } end end rescue StandardError => e Labimotion.log_exception(e, current_user) return { status: 'error', message: e. } end |