Module: Stagehand::Production

Extended by:
Production
Included in:
Production
Defined in:
lib/stagehand/production.rb,
lib/stagehand/production/controller.rb

Defined Under Namespace

Modules: Controller Classes: Record

Instance Method Summary collapse

Instance Method Details

#delete(staging_record, table_name = nil) ⇒ Object



32
33
34
# File 'lib/stagehand/production.rb', line 32

def delete(staging_record, table_name = nil)
  lookup(staging_record, table_name).delete_all
end

#exists?(staging_record, table_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/stagehand/production.rb', line 36

def exists?(staging_record, table_name = nil)
  lookup(staging_record, table_name).exists?
end

#lookup(staging_record, table_name = nil) ⇒ Object

Returns a scope that limits results any occurrences of the specified record. Record can be specified by passing a staging record, or an id and table_name.



49
50
51
52
53
# File 'lib/stagehand/production.rb', line 49

def lookup(staging_record, table_name = nil)
  table_name, id = Stagehand::Key.generate(staging_record, :table_name => table_name)
  prepare_to_modify(table_name)
  return Record.where(:id => id)
end

#modified?(staging_record) ⇒ Boolean

Returns true if the staging record’s attributes are different from the production record’s attributes Returns true if the staging_record does not exist on production Returns false if the staging record is identical to the production record

Returns:

  • (Boolean)


43
44
45
# File 'lib/stagehand/production.rb', line 43

def modified?(staging_record)
  production_record_attributes(staging_record) != staging_record_attributes(staging_record)
end

#save(staging_record) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stagehand/production.rb', line 18

def save(staging_record)
  attributes = staging_record_attributes(staging_record)

  return unless attributes.present?

  is_new = lookup(staging_record).update_all(attributes).zero?

  # Ensure we always return a record, even when updating instead of creating
  Record.new.tap do |record|
    record.assign_attributes(attributes)
    record.save if is_new
  end
end

#status(staging_record) ⇒ Object

Outputs a symbol representing the status of the staging record as it exists in the production database



8
9
10
11
12
13
14
15
16
# File 'lib/stagehand/production.rb', line 8

def status(staging_record)
  if !exists?(staging_record)
    :new
  elsif modified?(staging_record)
    :modified
  else
    :not_modified
  end
end