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



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

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)


48
49
50
# File 'lib/stagehand/production.rb', line 48

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

#find(*args) ⇒ Object



59
60
61
# File 'lib/stagehand/production.rb', line 59

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.



65
66
67
68
69
# File 'lib/stagehand/production.rb', line 65

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)


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

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
38
39
40
# File 'lib/stagehand/production.rb', line 26

def write(staging_record, attributes, table_name = nil)
  table_name, id = Stagehand::Key.generate(staging_record, :table_name => table_name)

  production_record = Connection.with_production_writes do
    prepare_to_modify(table_name)

    if update(table_name, id, attributes).nonzero?
      Record.find(id)
    else
      Record.find(insert(table_name, attributes))
    end
  end

  return production_record
end