artifact 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/artifact.rb CHANGED
@@ -15,6 +15,9 @@ module Artifact
15
15
 
16
16
  def self.configure
17
17
  yield config
18
+ unless config.author_name && config.author_email
19
+ raise "Both author_name and author_email are needed."
20
+ end
18
21
  end
19
22
 
20
23
  def self.root
@@ -57,7 +60,8 @@ module Artifact
57
60
  class Config
58
61
 
59
62
  attr_accessor *%w(
60
- root source_root posts_path drafts_path uploads_path directory_indexes
63
+ root source_root posts_path drafts_path uploads_path
64
+ directory_indexes rebuild_args author_name author_email
61
65
  )
62
66
 
63
67
  def initialize
@@ -67,6 +71,7 @@ module Artifact
67
71
  @drafts_path = 'drafts'
68
72
  @uploads_path = 'uploads'
69
73
  @directory_indexes = true
74
+ @rebuild_args = ''
70
75
  end
71
76
 
72
77
  end
@@ -183,7 +188,10 @@ module Artifact
183
188
  end
184
189
  end
185
190
 
191
+ # the author signature is a Hash containing :name, :email of the author
192
+ # and :time of the change.
186
193
  def write_commit(message, author)
194
+ author[:time] = Time.now
187
195
  commit_sha = Rugged::Commit.create(@git,
188
196
  :author => author,
189
197
  :message => message,
@@ -296,10 +304,10 @@ module Artifact
296
304
  end
297
305
 
298
306
  def update(content, meta = {})
299
- yaml_data = sanitized_meta(meta)
307
+ yaml_data = sanitized_meta(meta) # insert date and author if missing
300
308
  body = "#{yaml_data}---\n\n#{content}"
301
309
  super(body)
302
- @data = nil
310
+ @data = nil # force reload
303
311
  end
304
312
 
305
313
  def update_meta(key, val)
@@ -322,6 +330,7 @@ module Artifact
322
330
  read.split("---\n\n")
323
331
  end
324
332
 
333
+ # include existing date and author in yaml hash in case they're not set
325
334
  def sanitized_meta(hash)
326
335
  hash.stringify_keys!
327
336
  if exists?
data/lib/artifact/app.rb CHANGED
@@ -34,12 +34,10 @@ module Artifact
34
34
 
35
35
  private
36
36
 
37
- # FIXME: SOON!
38
37
  def current_user
39
- author = {
40
- :email => "ofofofof@gmail.com",
41
- :time => Time.now,
42
- :name => "The User"
38
+ {
39
+ :name => Artifact.config.author_name,
40
+ :email => Artifact.config.author_email
43
41
  }
44
42
  end
45
43
 
@@ -87,6 +87,7 @@ module Artifact
87
87
  view 'posts/new'
88
88
  end
89
89
 
90
+ # create new draft (non-existing file)
90
91
  post '/posts' do
91
92
  data = params[:meta] # includes title and maybe something else
92
93
  filename = data[:title].parameterize
@@ -107,26 +108,31 @@ module Artifact
107
108
  view 'posts/edit'
108
109
  end
109
110
 
111
+ # save, and optionally publish or unpublish post
110
112
  post '/posts/*' do
111
113
  @post = MarkdownFile.new(params[:splat][0])
112
114
 
113
- meta = (params[:meta] || {}).merge('last_updated_by' => current_user[:email])
115
+ # ensure date is valid and insert author/last_updated_by
116
+ meta = check_post_meta! or halt(422, "Invalid meta.")
117
+
118
+ # puts "Writing content and meta to file: #{meta.inspect}"
114
119
  @post.update(params[:content].gsub("\r\n", "\n"), meta)
115
120
 
116
- if params[:save] # user clicked on 'save' or 'published'
121
+ if params[:save] # user clicked on 'save', 'publish' or 'unpublish' (not auto-save)
122
+
123
+ # save changes to repo!
117
124
  # if draft was previously saved this will raise 'coz no changes.
118
125
  Artifact.repo.save(@post.path, current_user, false)
119
126
 
120
127
  if params[:save] == 'Publish'
121
- @post.update_meta(:date, Time.now.utc.strftime("%Y-%m-%d %H:%M %Z"))
122
128
  publish_post!(@post)
123
129
  elsif params[:save] == 'Unpublish'
124
130
  unpublish_post!(@post)
125
131
  end
126
132
  end
127
133
 
128
- updated!
129
- redirect to('/posts')
134
+ updated! # force middleman to rebuild resource list
135
+ redirect to('/posts') unless request.xhr?
130
136
  end
131
137
 
132
138
  delete '/posts/*' do
@@ -160,13 +166,31 @@ module Artifact
160
166
  file.update(tmpfile.read)
161
167
  Artifact.repo.save(file.path, current_user) # pass relative path
162
168
 
163
- updated!
169
+ updated! # force middleman to rebuild resource list
164
170
  redirect to('/')
165
171
  end
166
172
  end
167
173
 
168
174
  private
169
175
 
176
+ def check_post_meta!
177
+ meta = (params[:meta] || {}).symbolize_keys.merge(:last_updated_by => current_user[:email])
178
+
179
+ if meta[:date]
180
+ puts "Meta date is #{meta[:date]}"
181
+ return false unless valid_date?(meta[:date])
182
+ else # no date, set current
183
+ meta[:date] = Time.now.utc.strftime("%Y-%m-%d %H:%M %Z")
184
+ end
185
+
186
+ meta[:author] = current_user[:name] unless meta[:author]
187
+ meta
188
+ end
189
+
190
+ def valid_date?(str)
191
+ Time.parse(str) rescue false
192
+ end
193
+
170
194
  def get_uploads
171
195
  @uploads = Artifact.uploads.all.last(10).reverse
172
196
  end
@@ -190,7 +214,7 @@ module Artifact
190
214
  end
191
215
 
192
216
  def rebuild!
193
- puts `bundle exec middleman build`
217
+ puts `bundle exec middleman build #{Artifact.config.rebuild_args}`
194
218
  end
195
219
 
196
220
  def updated!
@@ -1,3 +1,3 @@
1
1
  module Artifact
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  <table class="right table" id="post-meta" style="display: none">
2
2
  <% @post.data.each do |key, val| %>
3
- <% next if %w(title date last_updated_by).include?(key.to_s) %>
3
+ <% next if %w(title last_updated_by).include?(key.to_s) %>
4
4
  <tr>
5
5
  <td><input type="text" value="<%= key %>" disabled="disabled" /></td>
6
6
  <td><input id="post-#{key}" type="text" name="meta[<%= key %>]" value="<%= val %>" /></td>
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Tom\xC3\xA1s Pollak"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2015-06-05 00:00:00 -07:00
17
+ date: 2015-06-07 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency