Class: TransportationRoute

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/transportation_route.rb

Overview

create_table :transportation_routes do |t|

t.string :internal_identifier
t.string :description
t.string :comments

t.string :external_identifier
t.string :external_id_source

t.timestamps

end

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.associated_modelsObject

Returns the value of attribute associated_models.



25
26
27
# File 'app/models/transportation_route.rb', line 25

def associated_models
  @associated_models
end

Instance Attribute Details

#associated_records_arrayObject

declare array to related models



21
22
23
# File 'app/models/transportation_route.rb', line 21

def associated_records_array
  @associated_records_array
end

Class Method Details

.open_entriesObject



55
56
57
58
59
# File 'app/models/transportation_route.rb', line 55

def open_entries
  joins(:segments)
      .where(transportation_routes: {manual_entry: false})
      .where(transportation_route_segments: {actual_arrival: nil})
end

.scope_by_dba_organization(dba_organization) ⇒ ActiveRecord::Relation Also known as: scope_by_dba

scope by dba organization

Parameters:

  • dba_organization (Party)

    dba organization to scope by

Returns:

  • (ActiveRecord::Relation)


74
75
76
# File 'app/models/transportation_route.rb', line 74

def scope_by_dba_organization(dba_organization)
  scope_by_party(dba_organization, {role_types: ['dba_org']})
end

.scope_by_party(party, options = {}) ⇒ ActiveRecord::Relation

scope by party

or an array of Party ids

Parameters:

  • party (Integer | Party | Array)

    either a id of Party record, a Party record, an array of Party records

  • options (Hash) (defaults to: {})

    options to apply to this scope

Options Hash (options):

  • :role_types (Array)

    role types to include in the scope

Returns:

  • (ActiveRecord::Relation)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/transportation_route.rb', line 99

def scope_by_party(party, options={})
  table_alias = String.random

  if options[:role_types]
    joins("inner join entity_party_roles as #{table_alias} on #{table_alias}.entity_record_type = 'TransportationRoute'
                                 and #{table_alias}.entity_record_id = transportation_routes.id and
                                 #{table_alias}.role_type_id in (#{RoleType.find_child_role_types(options[:role_types]).collect(&:id).join(',')})
                                 and #{table_alias}.party_id in (#{Party.select('id').where(id: party).to_sql})")

  else
    joins("inner join entity_party_roles as #{table_alias} on #{table_alias}.entity_record_type = 'TransportationRoute'
                                 and #{table_alias}.entity_record_id = transportation_routes.id
                                 and #{table_alias}.party_id in (#{Party.select('id').where(id: party).to_sql})")
  end
end

.scope_by_user(user, options = {}) ⇒ ActiveRecord::Relation

scope by work efforts assigned to the passed user

Parameters:

  • user (User)

    user to look for assignments

  • options (Hash) (defaults to: {})

    options to apply to this scope

Options Hash (options):

  • :role_types (Array)

    role types to include in the scope

Returns:

  • (ActiveRecord::Relation)


87
88
89
# File 'app/models/transportation_route.rb', line 87

def scope_by_user(user, options={})
  scope_by_party(user.party, options)
end

Instance Method Details

#associated_recordsObject

Gets all associated records (of any class) tied to this route



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/transportation_route.rb', line 117

def associated_records
  #used the declared instance variable array
  records = self.send("associated_records_array")
  records = records || []
  self.class.associated_models.each do |model|
    records = records | self.send(model.to_s)
  end

  #set it back to the instance variable
  self.send("associated_records_array=", records)

  records
end

#modify_stops(segment) ⇒ Object

Ties a segment’s from/to stops to its route, and then forces a reload of the route’s stops array from its cached value



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/transportation_route.rb', line 132

def modify_stops(segment)
  stops = []
  stops << segment.from_stop << segment.to_stop

  stops.each do |stop|
    unless stop.nil? or stop.route == self
      stop.route = self
      stop.save
    end
  end

  # Force reload of the stops array since it has changed
  self.stops(true)
end

#to_data_hashObject



147
148
149
150
151
152
153
154
# File 'app/models/transportation_route.rb', line 147

def to_data_hash
  data = to_hash(only: [:id, :internal_identifier, :description,
                        :comments, :created_at, :updated_at])

  data[:transportation_route_segments] = segments.collect { |item| item.to_data_hash }

  data
end