robin_cms 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7496d517d8e733bc0e0876e806347a0c3670efc489d974b16718e3b015d0a8e1
4
- data.tar.gz: cc6df56fde6950e75fddf3fef947e5363cdc67f506bd07c16f5bbe0f1940d629
3
+ metadata.gz: d3a49ee1ef9d638a300e82352bba65071dc6dfffecfb4cb409fbf8cbb8fbdd69
4
+ data.tar.gz: 928887dfc693144ebaaf9f2233a08b161faa7146ab7a037501098052d046fe74
5
5
  SHA512:
6
- metadata.gz: 6f4c01cfe9c26b0c7e864af98d405dfe55fbe986f78a3ee9295cb20e9c6bd808bbd9473685ddd17c158574540a63535c16a783a650a937f3e6419f215a81c51f
7
- data.tar.gz: 36e240374f3bcf0c564d15347b4847a6b781d7b6192b3b3214fbf35f297748415d1729773039dca5f61b00ce088d681d7b3b02ac340fc3aa1f981c2fa4dd63dc
6
+ metadata.gz: 17bcd5f6cc1d057c6740fb3d7cf393432cd823956fcf4e78421b085cfc1ec3ef382c40b776b5f570cfb295af6cae71fd8a0711e5accc118641509bc519947bd9
7
+ data.tar.gz: e7f2f736f7535dce94edbe15a41c24201959e1231d0827c542ec3b4560215d1285e49f1d94321a57bdca50c1cfd85015f8a11f74cbd952b967081e7f5b3291a3
data/lib/robin_cms/cms.rb CHANGED
@@ -53,7 +53,6 @@ module RobinCMS
53
53
 
54
54
  post "/login" do
55
55
  authenticate!(params[:username], params[:password])
56
-
57
56
  session[:auth_user] = params[:username]
58
57
  redirect to(home_page)
59
58
  rescue AuthenticationError
@@ -83,11 +82,9 @@ module RobinCMS
83
82
  end
84
83
 
85
84
  authenticate!(session[:auth_user], params[:old_password])
86
-
87
85
  update_credentials(params[:new_password])
88
86
  session[:auth_user] = nil
89
87
  flash[:success] = "Password updated succesfully. Please log in with your new credentials."
90
-
91
88
  redirect to("/login")
92
89
  rescue AuthenticationError
93
90
  flash[:error] = "Incorrect username or password"
@@ -100,69 +97,67 @@ module RobinCMS
100
97
  published: params[:published],
101
98
  q: params[:q]
102
99
  )
103
-
104
100
  erb :library
105
101
  end
106
102
 
107
103
  get "/libraries/:kind/item" do
108
- if params[:id]
109
- @item = @library.find_one(params[:id])
110
- halt 404 unless @item
111
- else
112
- @item = @library.blank
113
- end
104
+ @item = @library.blank
105
+ erb :library_item
106
+ end
114
107
 
108
+ get "/libraries/:kind/item/:id" do
109
+ @item = @library.find_one(params[:id])
110
+ halt 404 unless @item
115
111
  erb :library_item
116
112
  end
117
113
 
118
114
  post "/libraries/:kind/item" do
119
- if params[:id]
120
- @item = @library.find_one(params[:id])
121
- halt 404 unless @item
122
-
123
- if params[:image]
124
- filename = params[:image][:filename]
125
- tempfile = params[:image][:tempfile].to_path
126
-
127
- @library.delete_static(@item.attributes[:image_src])
128
- static_path = @library.create_static(filename, tempfile)
129
- params[:image_src] = "/" + static_path
130
- end
131
-
132
- @item.attributes = params
133
- @library.write(@item)
134
- else
135
- if params[:image]
136
- filename = params[:image][:filename]
137
- tempfile = params[:image][:tempfile].to_path
138
-
139
- static_path = @library.create_static(filename, tempfile)
140
- params[:image_src] = "/" + static_path
141
- end
142
-
143
- @library.create(params)
115
+ if params[:image]
116
+ static_path = @library.create_static(
117
+ params[:image][:filename],
118
+ params[:image][:tempfile].to_path
119
+ )
120
+ params[:image_src] = "/" + static_path
144
121
  end
145
122
 
123
+ @library.create(params)
146
124
  redirect to("/libraries/#{params[:kind]}")
147
125
  rescue ItemExistsError
148
126
  flash[:error] = "An item with the same name already exists"
149
127
  redirect to("/libraries/#{params[:kind]}/item")
150
128
  end
151
129
 
152
- post "/libraries/:kind/item/delete" do
153
- if params[:id]
154
- @item = @library.find_one(params[:id])
155
- halt 404 unless @item
130
+ post "/libraries/:kind/item/:id" do
131
+ @item = @library.find_one(params[:id])
132
+ halt 404 unless @item
156
133
 
157
- if @item.attributes[:image_src]
134
+ if params[:image]
135
+ # If there is an existing image for this item, delete it to prevent
136
+ # accumulation of old files.
137
+ if @item.attributes[:image_src] && !@item.attributes[:image_src].empty?
158
138
  @library.delete_static(@item.attributes[:image_src])
159
139
  end
140
+ static_path = @library.create_static(
141
+ params[:image][:filename],
142
+ params[:image][:tempfile].to_path
143
+ )
144
+ params[:image_src] = "/" + static_path
145
+ end
160
146
 
161
- @library.delete(@item.id)
162
- else
163
- halt 404
147
+ @item.attributes = params
148
+ @library.write(@item)
149
+ redirect to("/libraries/#{params[:kind]}")
150
+ end
151
+
152
+ post "/libraries/:kind/item/:id/delete" do
153
+ @item = @library.find_one(params[:id])
154
+ halt 404 unless @item
155
+
156
+ if @item.attributes[:image_src]
157
+ @library.delete_static(@item.attributes[:image_src])
164
158
  end
165
159
 
160
+ @library.delete(@item.id)
166
161
  redirect to("/libraries/#{params[:kind]}")
167
162
  end
168
163
 
@@ -4,31 +4,37 @@ module RobinCMS
4
4
  # This module provides methods for editing image files.
5
5
  # No methods are required in the base class in order to use this mixin.
6
6
  module Editable
7
- def resize_image(filepath, dimensions)
7
+ def resize_image(filepath, to:)
8
8
  # The mogrify command edits images in place. For more info, see
9
9
  # mogrify(1).
10
-
11
- system("mogrify -resize #{dimensions} #{filepath}")
10
+ system("mogrify -resize #{to} #{filepath}")
12
11
  if $?.exitstatus != 0
13
12
  raise ConversionError, "Could not resize image #{filepath}"
14
13
  end
15
14
  end
16
15
 
17
- def format_image(filepath, filetype)
18
- system("mogrify -format #{filetype} #{filepath}")
16
+ def convert_image(filepath, to:)
17
+ system("mogrify -format #{to} #{filepath}")
19
18
  if $?.exitstatus != 0
20
19
  raise ConversionError, "Could not format image #{filepath}"
21
20
  end
22
21
 
23
- # The name of the converted file will be the same as the original but
24
- # with a new file extension.
25
- converted = filepath.sub(/#{File.extname(filepath)}$/, ".#{filetype}")
22
+ # The file created by the mogrify command will be the same as the
23
+ # original but with the new file extension.
24
+ converted = filepath.sub(/#{File.extname(filepath)}$/, ".#{to}")
26
25
 
27
- # Mogrify's format command creates a new file with the new extension.
28
- # Copy the contents of this file to the original, then delete the newly
29
- # created file.
30
- IO.copy_stream(converted, filepath)
31
- File.delete(converted)
26
+ if converted == filepath
27
+ # If the converted file has the same filename as the original (i.e. no
28
+ # conversion happened), there is nothing more to do.
29
+ filepath
30
+ else
31
+ # Otherwise, copy the contents of the mogrify output file to a new
32
+ # tempfile and delete the mogrify output.
33
+ tempfile = Tempfile.new(["robin_cms" ".#{to}"])
34
+ IO.copy_stream(converted, tempfile)
35
+ File.delete(converted)
36
+ tempfile
37
+ end
32
38
  end
33
39
  end
34
40
  end
@@ -42,6 +42,7 @@ module RobinCMS
42
42
 
43
43
  def create_static(filename, tempfile)
44
44
  path = File.join(@schema[:static_location], File.basename(make_slug(filename)))
45
+ FileUtils.mkdir_p(File.dirname(path))
45
46
 
46
47
  if File.exist?(path)
47
48
  raise ItemExistsError, "An item with the same name already exists"
@@ -51,18 +52,24 @@ module RobinCMS
51
52
  dimensions = image_field&.dig(:dimensions)
52
53
  filetype = image_field&.dig(:filetype)
53
54
 
54
- resize_image(tempfile, dimensions) if dimensions
55
- format_image(tempfile, filetype) if filetype
55
+ resize_image(tempfile, to: dimensions) if dimensions
56
56
 
57
- FileUtils.mkdir_p(File.dirname(path))
58
- FileUtils.cp(tempfile, path)
57
+ # Only convert if the specified filetype is different to the actual
58
+ # filetype. Unlike resize_image which modifies the image in-pace, this
59
+ # function necessarily creates a new file, so we need to ensure we
60
+ # update the path with the new file extension.
61
+ if filetype && File.extname(tempfile) != ".#{filetype}"
62
+ tempfile = convert_image(tempfile, to: filetype)
63
+ path = File.join(File.dirname(path), "#{File.basename(path, '.*')}.#{filetype}")
64
+ end
59
65
 
66
+ FileUtils.cp(tempfile, path)
60
67
  path
61
68
  end
62
69
 
63
70
  def delete_static(filename)
71
+ return if filename.nil? || filename.empty?
64
72
  path = File.join(@schema[:static_location], File.basename(filename))
65
-
66
73
  return unless File.exist?(path)
67
74
 
68
75
  File.delete(path)
@@ -79,5 +86,9 @@ module RobinCMS
79
86
  def [](key)
80
87
  @schema[key]
81
88
  end
89
+
90
+ def kind
91
+ @schema[:id]
92
+ end
82
93
  end
83
94
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RobinCMS
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -9,7 +9,7 @@
9
9
  <button type="submit">Close</button>
10
10
  </form>
11
11
  <form
12
- action='<%= url("/libraries/#{@library[:id]}/item/delete?id=#{@item.id}") %>'
12
+ action='<%= url("/libraries/#{@library.kind}/item/#{@item.id}/delete") %>'
13
13
  method="post"
14
14
  >
15
15
  <button type="submit" class="--danger">Delete</button>
@@ -33,5 +33,5 @@
33
33
  placeholder="Search <%= @library[:label].downcase %>..."
34
34
  />
35
35
  <button type="submit">Apply</button>
36
- <a href="/admin/libraries/<%= @library[:id] %>">Clear filters</a>
36
+ <a href="/admin/libraries/<%= @library.kind %>">Clear filters</a>
37
37
  </form>
@@ -6,7 +6,7 @@
6
6
  <p><%= @library[:description] %></p>
7
7
  <% end %>
8
8
  <% if @library[:can_create] %>
9
- <form action='<%= url("/libraries/#{@library[:id]}/item") %>'>
9
+ <form action='<%= url("/libraries/#{@library.kind}/item") %>'>
10
10
  <button type="submit">
11
11
  New <%= @library[:label_singular].downcase %>
12
12
  </button>
@@ -20,8 +20,8 @@
20
20
  <br />
21
21
  <br />
22
22
  <% if @library[:can_create] %>
23
- Create a <a href='<%= url("/libraries/#{@library[:id]}/item") %>'>new <%= @library[:label_singular].downcase %></a>
24
- or try <a href='<%= url("/libraries/#{@library[:id]}") %>'>clearing</a> your search filters.
23
+ Create a <a href='<%= url("/libraries/#{@library.kind}/item") %>'>new <%= @library[:label_singular].downcase %></a>
24
+ or try <a href='<%= url("/libraries/#{@library.kind}") %>'>clearing</a> your search filters.
25
25
  <% end %>
26
26
  </div>
27
27
  <% else %>
@@ -40,13 +40,13 @@
40
40
  <% for item in @items %>
41
41
  <tr>
42
42
  <td>
43
- <a href='<%= url("/libraries/#{@library[:id]}/item?id=#{item.id}") %>'>
43
+ <a href='<%= url("/libraries/#{@library.kind}/item/#{item.id}") %>'>
44
44
  <%= item.display_name %>
45
45
  </a>
46
46
  </td>
47
47
  <% if @library.drafts_enabled? %>
48
48
  <td>
49
- <a href='<%= url("/libraries/#{@library[:id]}/item?id=#{item.id}") %>'>
49
+ <a href='<%= url("/libraries/#{@library.kind}/item/#{item.id}") %>'>
50
50
  <span class="badge --<%= item.published? ? 'published' : 'draft' %>">
51
51
  <%= item.published_label %>
52
52
  </span>
@@ -54,12 +54,12 @@
54
54
  </td>
55
55
  <% end %>
56
56
  <td>
57
- <a href='<%= url("/libraries/#{@library[:id]}/item?id=#{item.id}") %>'>
57
+ <a href='<%= url("/libraries/#{@library.kind}/item/#{item.id}") %>'>
58
58
  <%= item.created_at.strftime("%b %d, %Y") %>
59
59
  </a>
60
60
  </td>
61
61
  <td>
62
- <a href='<%= url("/libraries/#{@library[:id]}/item?id=#{item.id}") %>'>
62
+ <a href='<%= url("/libraries/#{@library.kind}/item/#{item.id}") %>'>
63
63
  <%= item.updated_at.strftime("%b %d, %Y") %>
64
64
  </a>
65
65
  </td>
@@ -1,5 +1,5 @@
1
1
  <span class="controls">
2
- <a href='<%= url("/libraries/#{@library[:id]}") %>'>Back</a>
2
+ <a href='<%= url("/libraries/#{@library.kind}") %>'>Back</a>
3
3
  <button type="submit">Save</button>
4
4
  <% if has_delete %>
5
5
  <%= erb :delete_dialog %>
@@ -7,9 +7,9 @@
7
7
  <form
8
8
  class="card"
9
9
  <% if @item.id %>
10
- action='<%= url("/libraries/#{@library[:id]}/item?id=#{@item.id}") %>'
10
+ action='<%= url("/libraries/#{@library.kind}/item/#{@item.id}") %>'
11
11
  <% else %>
12
- action='<%= url("/libraries/#{@library[:id]}/item") %>'
12
+ action='<%= url("/libraries/#{@library.kind}/item") %>'
13
13
  <% end %>
14
14
  method="post"
15
15
  enctype="multipart/form-data"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robin_cms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aron Lebani
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-09-22 00:00:00.000000000 Z
11
+ date: 2025-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt