Module: Labimotion::ElementHelpers
- Extended by:
- Grape::API::Helpers
- Defined in:
- lib/labimotion/helpers/element_helpers.rb
Overview
ElementHelpers
Instance Method Summary collapse
- #create_element(current_user, params) ⇒ Object
- #create_element_klass(current_user, params) ⇒ Object
- #element_revisions(params) ⇒ Object
- #klass_list(is_generic_only) ⇒ Object
- #list_serialized_elements(params, current_user) ⇒ Object
- #list_user_elements(scope, params) ⇒ Object
- #update_element_by_id(current_user, params) ⇒ Object
- #update_element_klass(current_user, params) ⇒ Object
- #upload_generics_files(current_user, params) ⇒ Object
Instance Method Details
#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 |
# 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]['eln'] = Chemotion::Application.config.version params[:properties]['labimotion'] = Labimotion::VERSION params[:properties]['klass'] = 'Element' properties = params[:properties] properties.delete('flow') unless properties['flow'].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(element, params[:properties], current_user) element.container = update_datamodel(params[:container], current_user) element.save! element.save_segments(segments: params[:segments], current_user_id: current_user.id) element rescue => e Labimotion.log_exception(e, current_user) raise e end |
#create_element_klass(current_user, params) ⇒ Object
19 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 19 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]['uuid'] = uuid if attributes[:properties_template].present? attributes[:properties_template] = template unless attributes[:properties_template].present? attributes[:properties_template]['eln'] = Chemotion::Application.config.version if attributes[:properties_template].present? attributes[:properties_template]['labimotion'] = Labimotion::VERSION if attributes[:properties_template].present? attributes[:properties_template]['klass'] = 'ElementKlass' if attributes[:properties_template].present? 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.id) klass_names_file = Rails.root.join('config', 'klasses.json') klasses = Labimotion::ElementKlass.where(is_active: true)&.pluck(:name) || [] File.write(klass_names_file, klasses) klasses rescue => e Labimotion.log_exception(e, current_user) raise e end |
#element_revisions(params) ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 181 def element_revisions(params) klass = Labimotion::Element.find(params[:id]) list = klass.elements_revisions unless klass.nil? list&.sort_by(&:created_at)&.reverse rescue => e Labimotion.log_exception(e, current_user) raise e end |
#klass_list(is_generic_only) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 11 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
230 231 232 233 234 235 236 237 238 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 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 230 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).order('created_at DESC') 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 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 => e Labimotion.log_exception(e, current_user) raise e end |
#list_user_elements(scope, params) ⇒ Object
190 191 192 193 194 195 196 197 198 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 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 190 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('layers', layer, '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 => e Labimotion.log_exception(e, current_user) raise e end |
#update_element_by_id(current_user, params) ⇒ Object
105 106 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 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 105 def update_element_by_id(current_user, params) element = Labimotion::Element.find(params[:id]) update_datamodel(params[:container], current_user) properties = update_sample_association(element, params[:properties], current_user) params.delete(:container) params.delete(:properties) attributes = declared(params.except(:segments), include_missing: false) properties['eln'] = Chemotion::Application.config.version properties['labimotion'] = Labimotion::VERSION 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('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 => 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 => e Labimotion.log_exception(e, current_user) raise e end |
#upload_generics_files(current_user, params) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 |
# File 'lib/labimotion/helpers/element_helpers.rb', line 136 def upload_generics_files(current_user, params) attach_ary = [] att_ary = create_uploads( '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( '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], params[:att_type], params[:att_id], params[:attfilesIdentifier], current_user.id ) end (attach_ary << att_ary).flatten! unless att_ary&.empty? if Labimotion::IS_RAILS5 == true TransferThumbnailToPublicJob.set(queue: "transfer_thumbnail_to_public_#{current_user.id}").perform_now(attach_ary) unless attach_ary.empty? TransferFileFromTmpJob.set(queue: "transfer_file_from_tmp_#{current_user.id}").perform_now(attach_ary) unless attach_ary.empty? end true rescue => e Labimotion.log_exception(e, current_user) ## false raise e end |