Class: Listing

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/listing.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: listings

conditions   :text
created_at   :datetime         not null
creator_id   :integer
description  :text
id           :integer          not null, primary key
lock_version :integer          default(0), not null
mail         :text
name         :string           not null
query        :text
root_model   :string           not null
source       :text
story        :text
updated_at   :datetime         not null
updater_id   :integer

Instance Method Summary collapse

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Instance Method Details

#can_mail?Boolean

Returns:

  • (Boolean)

131
132
133
# File 'app/models/listing.rb', line 131

def can_mail?
  coordinate_columns.any?
end

#compute_whereObject


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/listing.rb', line 97

def compute_where
  conn = self.class.connection
  c = ''
  if klass = begin
               root_model.classify.constantize
             rescue
               nil
             end
    if klass.columns_definition[:type] && klass.table_name != klass.name.tableize
      c << "#{root.name}.type IN ('#{klass.name}'" + klass.descendants.map { |k| ", '#{k.name}'" }.join + ')'
    end
  end
  #  No reflections => no columns => no conditions
  return c unless reflection_nodes.any?
  # Filter on columns
  if filtered_columns.any?
    c << ' AND ' if c.present?
    c << filtered_columns.map(&:condition).join(' AND ')
  end
  # General conditions
  if conditions.present?
    c << ' AND ' if c.present?
    c << '(' + conditions + ')'
  end
  c
end

#duplicateObject

Fully duplicate a listing


125
126
127
128
129
# File 'app/models/listing.rb', line 125

def duplicate
  listing = self.class.create!(attributes.merge(name: :copy_of.tl(source: name)).delete_if { |a| %w[id lock_version].include?(a.to_s) })
  root_node.duplicate(listing)
  listing
end

#generateObject


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/listing.rb', line 77

def generate
  query = ''
  begin
    conn = self.class.connection
    root = self.root
    columns_to_export = exportable_columns.collect { |n| [n.position, "#{n.name} AS " + conn.quote_column_name(n.label)] }
    columns_to_export += custom_fields_columns.collect { |cf| [cf.position, "#{cf.name}' AS #{conn.quote_column_name(cf.label)}"] }
    columns_to_export = columns_to_export.sort_by(&:first).map(&:last)
    query = 'SELECT ' + columns_to_export.join(', ')
    query << " FROM #{root.model.table_name} AS #{root.name}" + root.compute_joins
    query << ' WHERE ' + compute_where if compute_where.present?
    unless columns_to_export.size.zero?
      query << ' ORDER BY ' + exportable_fields.map { |n| conn.quote_column_name(n.label) }.join(', ')
    end
  rescue
    query = ''
  end
  query
end

#rootObject


73
74
75
# File 'app/models/listing.rb', line 73

def root
  root_node || nodes.create!(label: root_model_name, name: root_model, nature: 'root')
end

#root_model_nameObject


65
66
67
68
69
70
71
# File 'app/models/listing.rb', line 65

def root_model_name
  # Ekylibre::Record.human_name(self.root_model.underscore)

  root_model.classify.constantize.model_name.human
rescue
  '???'
end