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



39
40
41
42
43
# File 'lib/stagehand/production.rb', line 39

def delete(staging_record, table_name = nil)
  Connection.with_production_writes do
    matching(staging_record, table_name).delete_all
  end
end

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/stagehand/production.rb', line 45

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

#find(*args) ⇒ Object



56
57
58
# File 'lib/stagehand/production.rb', line 56

def find(*args)
  matching(*args).first
end

#matching(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.



62
63
64
65
66
# File 'lib/stagehand/production.rb', line 62

def matching(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, table_name = nil) ⇒ 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)


52
53
54
# File 'lib/stagehand/production.rb', line 52

def modified?(staging_record, table_name = nil)
  production_record_attributes(staging_record, table_name) != staging_record_attributes(staging_record, table_name)
end

#save(staging_record, table_name = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/stagehand/production.rb', line 18

def save(staging_record, table_name = nil)
  attributes = staging_record_attributes(staging_record, table_name)

  return unless attributes.present?

  write(staging_record, attributes, table_name)
end

#status(staging_record, table_name = nil) ⇒ 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, table_name = nil)
  if !exists?(staging_record, table_name)
    :new
  elsif modified?(staging_record, table_name)
    :modified
  else
    :not_modified
  end
end

#write(staging_record, attributes, table_name = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/stagehand/production.rb', line 26

def write(staging_record, attributes, table_name = nil)
  Connection.with_production_writes do
    is_new = matching(staging_record, table_name).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.id = Stagehand::Key.generate(staging_record, :table_name => table_name).last unless record.id
      record.save if is_new
    end
  end
end