Class: Mist::GitModel

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming, ClassMethods
Includes:
ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Observing, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/mist/git_model.rb

Direct Known Subclasses

Post

Defined Under Namespace

Modules: ClassMethods Classes: Attributes

Instance Method Summary collapse

Methods included from ClassMethods

[], add_attribute_default, add_attribute_getter, add_attribute_setter, all, attribute, count, create!, exist?, extended, find, inherited, last, load, meta_file_path, record_path, save_meta_data, table_name, timestamps

Constructor Details

#initialize(attributes = {}) ⇒ GitModel

Returns a new instance of GitModel.



29
30
31
32
33
34
35
36
# File 'lib/mist/git_model.rb', line 29

def initialize(attributes = {})
  run_callbacks :initialize do
    default_attributes!
    attributes.each do |key, value|
      self.send(:"#{key}=", value)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
# File 'lib/mist/git_model.rb', line 42

def ==(other)
  id == (other.kind_of?(self.class) ? other.id : other)
end

#attributesObject



81
82
83
# File 'lib/mist/git_model.rb', line 81

def attributes
  @attributes ||= Mist::GitModel::Attributes.new(self)
end

#class_nameObject



38
39
40
# File 'lib/mist/git_model.rb', line 38

def class_name
  self.class.name
end

#commit(commit_message = self.commit_message) ⇒ Object



54
55
56
57
# File 'lib/mist/git_model.rb', line 54

def commit(commit_message = self.commit_message)
  Mist.repository.add path
  Mist.repository.commit commit_message
end

#commit_messageObject



46
47
48
49
50
51
52
# File 'lib/mist/git_model.rb', line 46

def commit_message
  if new_record?
    "Post created"
  else
    "Post updated"
  end
end

#default_attributes!Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mist/git_model.rb', line 85

def default_attributes!
  default_attributes.each do |key, value|
    if value.kind_of?(Proc)
      attributes[key] = instance_eval &value
    else
      attributes[key] = value
    end
  end
  
  changed_attributes.clear
end

#destroyObject



67
68
69
70
71
72
73
74
# File 'lib/mist/git_model.rb', line 67

def destroy
  return if new_record?
  run_callbacks :destroy do
    Mist.repository.remove path, :recursive => true
    Mist.repository.commit "Destroyed #{class_name} #{id.inspect}"
    FileUtils.rm_rf path
  end
end

#inspectObject



151
152
153
# File 'lib/mist/git_model.rb', line 151

def inspect
  "#<#{self.class.name} #{attributes.collect { |a| [a[0], a[1].inspect].join('=') }.join(' ')}>"
end

#new_record?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/mist/git_model.rb', line 101

def new_record?
  id_was.blank? || !File.file?(path_was)
end

#pathObject



59
60
61
# File 'lib/mist/git_model.rb', line 59

def path
  record_path id
end

#path_wasObject



63
64
65
# File 'lib/mist/git_model.rb', line 63

def path_was
  record_path id_was
end

#persisted?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/mist/git_model.rb', line 97

def persisted?
  !changed? && !new_record?
end

#saveObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mist/git_model.rb', line 105

def save
  return false unless valid?
  
  if new_record? || changed?
    create_or_update_callback = new_record? ? :create : :update
    run_callbacks create_or_update_callback do
      run_callbacks :save do
        Mist.repository.lib.mv path_was, path if !new_record? && id_changed?
        save_record_file
        commit
      end
    end
  end
  
  if !(files = Mist.repository.lib.diff_files).empty?
    # if false, there's nothing we can reliably do to sync up unless we
    # forcefully commit all changes, which the user may not want. However,
    # if true, then this record was probably renamed from id_on_record
    # to id. So, commit the deletion.
    old_path = record_path(id_on_record).to_s.sub(/^#{Regexp::escape Mist.repository_location.to_s}\/?/, '')
    if files.keys.include?(old_path) && files[old_path][:type] == 'D'
      save_record_file
      Mist.repository.lib.remove old_path
      Mist.repository.lib.add path
      Mist.repository.commit "Syncing to external filesystem changes"
    else
      Rails.logger.warn "Found uncommitted changes in git repo but didn't recognize them, so didn't commit them"
      # no need to show the diff in the log, as the Git library has done that for us
    end
  end
  
  @previously_changed = changes
  changed_attributes.clear
  true
end

#save!Object



147
148
149
# File 'lib/mist/git_model.rb', line 147

def save!
  raise "Record not saved: #{errors.full_messages.join('; ')}" unless save
end

#save_record_fileObject



141
142
143
144
145
# File 'lib/mist/git_model.rb', line 141

def save_record_file
  self.id_on_record = id
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, "w") { |f| f.print attributes.to_yaml }
end

#update_attributes(attributes) ⇒ Object



76
77
78
79
# File 'lib/mist/git_model.rb', line 76

def update_attributes(attributes)
  attributes.each { |k,v| self.send(:"#{k}=", v) }
  save
end