Module: FmStore::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/fm_store/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject Also known as: delete

Throws Rfm::Error::RecordAccessDeniedError if no permission to delete



65
66
67
68
69
70
71
72
# File 'lib/fm_store/persistence.rb', line 65

def destroy
  run_callbacks(:destroy) do
    unless @record_id.nil?
      conn = Connection.establish_connection(self.class)
      conn.delete(@record_id)
    end
  end
end

#saveObject

Instance methods



14
15
16
# File 'lib/fm_store/persistence.rb', line 14

def save
  create_or_update
end

#update_attributes(attributes = {}) ⇒ Object



18
19
20
21
22
23
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
# File 'lib/fm_store/persistence.rb', line 18

def update_attributes(attributes = {})
  if valid?
    attrs = {}

    attributes.each do |field, value|
      field = field.to_s

      fm_name = self.class.find_fm_name(field)
      type = self.class.find_fm_type(field)

      if fm_name
        if type == Date
          if value.blank?
            value = '' # clear the date
          else
            value = value.strftime("%m/%d/%Y")
          end
        elsif type == DateTime
          if value.blank?
            value = ''
          else
            value = value.strftime("%m/%d/%Y %H:%M:%S")
          end
        elsif type == Time
          if value.blank?
            value = ''
          else
            value = value.strftime("%H:%M")
          end
        end

        attrs[fm_name] = value
      end
    end

    run_callbacks(:save) do
      conn = Connection.establish_connection(self.class)
      result = conn.edit(@record_id, attrs)

      return FmStore::Builders::Single.build(result, self.class)
    end;self # just in case
  else
    false
  end
end