Class: Conveyance

Constant Summary collapse

GRAPH_ENTRY_POINTS =
[:asserted_distributions].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#end_timeNumeric

Returns in seconds.

Returns:

  • (Numeric)

    in seconds


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
106
107
108
109
110
111
# File 'app/models/conveyance.rb', line 24

class Conveyance < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::Notes
  include Shared::Tags
  include Shared::AssertedDistributions
  include Shared::PolymorphicAnnotator
  include Shared::IsData
  polymorphic_annotates(:conveyance_object)

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  acts_as_list scope: [:project_id, :conveyance_object_type, :conveyance_object_id]

  belongs_to :sound, inverse_of: :conveyances
  belongs_to :conveyance_object, polymorphic: true

  validates_presence_of :conveyance_object, :sound

  validates_uniqueness_of :sound_id, scope: [:conveyance_object_type, :conveyance_object_id]

  validate :end_time_after_start

  accepts_nested_attributes_for :sound

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return Conveyance.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('Conveyance')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = Conveyance.arel_table

    case used_on
    when 'AssertedDistribution'
      Conveyance.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  protected

  def end_time_after_start
    if (start_time && end_time) && start_time > end_time
      errors.add(:end_time, 'must be after start time')
    end
  end

end

Class Method Details

.select_optimized(user_id, project_id, klass) ⇒ Object

[View source]

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/conveyance.rb', line 84

def self.select_optimized(user_id, project_id, klass)
  r = used_recently(user_id, project_id, klass)
  h = {
    quick: [],
    pinboard: Conveyance.pinned_by(user_id).where(project_id: project_id).to_a,
    recent: []
  }

  if r.empty?
    h[:quick] = Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
  else
    h[:recent] = Conveyance.where('"conveyances"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
    h[:quick] = (Conveyance.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                 Conveyance.where('"conveyances"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
  end

  h
end

.used_recently(user_id, project_id, used_on) ⇒ Scope

Returns the max 10 most recently used.

Returns:

  • (Scope)

    the max 10 most recently used

[View source]

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/conveyance.rb', line 51

def self.used_recently(user_id, project_id, used_on)
  t = case used_on
      when 'AssertedDistribution'
        AssertedDistribution.arel_table
      else
        return Conveyance.none
      end

  # i is a select manager
  i = case used_on
      when 'AssertedDistribution'
        t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
          .where(
            t['updated_at'].gt(1.week.ago).and(
              t['asserted_distribution_object_type'].eq('Conveyance')
            )
          )
          .where(t['updated_by_id'].eq(user_id))
          .where(t['project_id'].eq(project_id))
          .order(t['updated_at'].desc)
      end

  z = i.as('recent_t')
  p = Conveyance.arel_table

  case used_on
  when 'AssertedDistribution'
    Conveyance.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
    ).pluck(:id).uniq
  end
end

Instance Method Details

#end_time_after_startObject (protected)

[View source]

105
106
107
108
109
# File 'app/models/conveyance.rb', line 105

def end_time_after_start
  if (start_time && end_time) && start_time > end_time
    errors.add(:end_time, 'must be after start time')
  end
end