robin_cms 0.1.1 → 0.1.3

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.
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RobinCMS
4
+ class Library
5
+ include Editable
6
+ include Sluggable
7
+ include Queryable
8
+
9
+ attr_reader :schema
10
+
11
+ def initialize(schema)
12
+ @schema = schema.freeze
13
+ end
14
+
15
+ def blank
16
+ Item.new(nil, **@schema)
17
+ end
18
+
19
+ def create(attributes)
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def find_one(id)
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def all
28
+ raise NotImplementedError
29
+ end
30
+
31
+ # Writes the specified item to disk.
32
+ #
33
+ # Returns the path where the item was written.
34
+ def write(item)
35
+ raise NotImplementedError
36
+ end
37
+
38
+ # Delete the item specified by +id+.
39
+ def delete(id)
40
+ raise NotImplementedError
41
+ end
42
+
43
+ def create_static(filename, tempfile)
44
+ path = File.join(@schema[:static_location], File.basename(make_slug(filename)))
45
+
46
+ if File.exist?(path)
47
+ raise ItemExistsError, "An item with the same name already exists"
48
+ end
49
+
50
+ image_field = @schema[:fields].find { |f| f[:type] == "image" }
51
+ dimensions = image_field&.dig(:dimensions)
52
+ filetype = image_field&.dig(:filetype)
53
+
54
+ resize_image(tempfile, dimensions) if dimensions
55
+ format_image(tempfile, filetype) if filetype
56
+
57
+ FileUtils.mkdir_p(File.dirname(path))
58
+ FileUtils.cp(tempfile, path)
59
+
60
+ path
61
+ end
62
+
63
+ def delete_static(filename)
64
+ path = File.join(@schema[:static_location], File.basename(filename))
65
+
66
+ return unless File.exist?(path)
67
+
68
+ File.delete(path)
69
+ end
70
+
71
+ def drafts_enabled?
72
+ @schema[:type].nil? || @schema[:type] == "collection"
73
+ end
74
+
75
+ def sorted_fields
76
+ @schema[:fields].sort_by { |field| field[:order] || Float::INFINITY }
77
+ end
78
+
79
+ def [](key)
80
+ @schema[key]
81
+ end
82
+ end
83
+ end
@@ -1,8 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RobinCMS
4
+ # This module provides methods for querying items.
5
+ #
6
+ # In order to use this as a mixin, you only need to implement an +all+
7
+ # method in the base class. This method should take no parameters, and
8
+ # return all items.
4
9
  module Queryable
5
- def where(library, **kwargs)
10
+ def query(**kwargs)
6
11
  by_published = lambda do |i|
7
12
  case kwargs[:published]
8
13
  when nil, ""
@@ -34,14 +39,11 @@ module RobinCMS
34
39
  end * sort_direction
35
40
  end
36
41
 
37
- all(library)
38
- .filter(&by_search)
39
- .filter(&by_published)
40
- .sort(&by_field)
42
+ all.filter(&by_search).filter(&by_published).sort(&by_field)
41
43
  end
42
44
 
43
- def count(library)
44
- all(library).size
45
+ def count
46
+ all.size
45
47
  end
46
48
  end
47
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RobinCMS
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -11,7 +11,7 @@
11
11
  </option>
12
12
  <% end %>
13
13
  </select>
14
- <% if @config.drafts_enabled?(@library) %>
14
+ <% if @library.drafts_enabled? %>
15
15
  <select id="published" name="published">
16
16
  <% for option in PUBLISHED_OPTIONS%>
17
17
  <option
@@ -2,5 +2,5 @@
2
2
  id="<%= safe_id(field[:id], 'field') %>"
3
3
  type="hidden"
4
4
  name="<%= field[:id] %>"
5
- value="<%= @item.attributes[field[:id].to_sym] || field[:default] %>"
5
+ value="<%= @item.field_value_or_default(field) %>"
6
6
  />
@@ -3,7 +3,7 @@
3
3
  id="<%= safe_id(field[:id], 'field') %>"
4
4
  type="<%= field[:type] %>"
5
5
  name="<%= field[:id] %>"
6
- value="<%= @item.attributes[field[:id].to_sym] || field[:default] %>"
6
+ value="<%= @item.field_value_or_default(field) %>"
7
7
  <% if field[:required] %>required<% end %>
8
8
  <% if field[:readonly] %>readonly<% end %>
9
9
  />
@@ -3,8 +3,11 @@
3
3
  <head>
4
4
  <meta charset="utf-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6
+ <meta name="robots" content="noindex,nofollow" />
6
7
  <title><%= @config[:title] %> | admin</title>
7
- <%= erb :style %>
8
+ <style>
9
+ <%= erb :"stylesheet.css" %>
10
+ </style>
8
11
  </head>
9
12
  <body <% if flash[:success_dialog] %>onload="successdialog.showModal()"<% end %>>
10
13
  <% if session[:auth_user] %>
@@ -14,7 +17,7 @@
14
17
  </span>
15
18
  <span class="controls --gap-md">
16
19
  <a href="<%= @config[:url] %>" target="_blank">
17
- <%= @config[:url] %><%= erb :new_tab %>
20
+ <%= @config[:url] %><%= erb :"new_tab.svg" %>
18
21
  </a>
19
22
  <% if @config[:build_command] %>
20
23
  <form id="publish-form" action="/admin/publish" method="post">
@@ -29,7 +32,7 @@
29
32
  <main id="site-content">
30
33
  <%= yield %>
31
34
  <footer id="site-footer" class="card --clear">
32
- <%= erb :logo %>
35
+ <%= erb :"logo.svg" %>
33
36
  <p>
34
37
  Made with 🧡
35
38
  <br />
@@ -29,7 +29,7 @@
29
29
  <thead>
30
30
  <tr>
31
31
  <th class="grow-column">Name</th>
32
- <% if @config.drafts_enabled?(@library) %>
32
+ <% if @library.drafts_enabled? %>
33
33
  <th>Status</th>
34
34
  <% end %>
35
35
  <th>Created</th>
@@ -44,7 +44,7 @@
44
44
  <%= item.display_name %>
45
45
  </a>
46
46
  </td>
47
- <% if @config.drafts_enabled?(@library) %>
47
+ <% if @library.drafts_enabled? %>
48
48
  <td>
49
49
  <a href='<%= url("/libraries/#{@library[:id]}/item?id=#{item.id}") %>'>
50
50
  <span class="badge --<%= item.published? ? 'published' : 'draft' %>">
@@ -1,4 +1,7 @@
1
1
  <span class="controls">
2
2
  <a href='<%= url("/libraries/#{@library[:id]}") %>'>Back</a>
3
3
  <button type="submit">Save</button>
4
+ <% if has_delete %>
5
+ <%= erb :delete_dialog %>
6
+ <% end %>
4
7
  </span>
@@ -2,7 +2,7 @@
2
2
  <h2>
3
3
  <% if @item.id %>Edit<% else %>New<% end %> <%= @library[:label_singular].downcase %>
4
4
  </h2>
5
- <%= erb :library_actions %>
5
+ <%= erb :library_actions, locals: { has_delete: @library[:can_delete] && @item.id } %>
6
6
  </header>
7
7
  <form
8
8
  class="card"
@@ -14,31 +14,26 @@
14
14
  method="post"
15
15
  enctype="multipart/form-data"
16
16
  >
17
- <% for field in @sorted_fields %>
17
+ <% @library.sorted_fields.each do |field| %>
18
18
  <div class="field">
19
19
  <% case field[:type]
20
20
  when "text", "date", "number", "color", "email", "url" %>
21
- <%= erb :input_field, locals: { :field => field } %>
21
+ <%= erb :input_field, locals: { field: field } %>
22
22
  <% when "hidden" %>
23
- <%= erb :hidden_field, locals: { :field => field } %>
23
+ <%= erb :hidden_field, locals: { field: field } %>
24
24
  <% when "select" %>
25
- <%= erb :select_field, locals: { :field => field } %>
25
+ <%= erb :select_field, locals: { field: field } %>
26
26
  <% when "image" %>
27
- <%= erb :image_field, locals: { :field => field } %>
27
+ <%= erb :image_field, locals: { field: field } %>
28
28
  <% when "richtext" %>
29
- <%= erb :richtext_field, locals: { :field => field } %>
29
+ <%= erb :richtext_field, locals: { field: field } %>
30
30
  <% else %>
31
31
  <div></div>
32
32
  <% end %>
33
33
  </div>
34
34
  <% end %>
35
35
  <%= erb :flash %>
36
- <footer class="controls --align-space">
37
- <% if @library[:can_delete] && @item.id %>
38
- <%= erb :delete_dialog %>
39
- <% else %>
40
- <span></span>
41
- <% end %>
42
- <%= erb :library_actions %>
36
+ <footer class="controls --align-right">
37
+ <%= erb :library_actions, locals: { has_delete: false } %>
43
38
  </footer>
44
39
  </form>
@@ -5,6 +5,6 @@
5
5
  id="richtext-content"
6
6
  type="hidden"
7
7
  name="<%= field[:id] %>"
8
- value="<%= @item.attributes[field[:id].to_sym] || field[:default] %>"
8
+ value="<%= @item.field_value_or_default(field) %>"
9
9
  >
10
10
  <trix-editor input="richtext-content"></trix-editor>
@@ -2,7 +2,7 @@
2
2
  <select
3
3
  id="<%= safe_id(field[:id], 'field') %>"
4
4
  name="<%= field[:id] %>"
5
- value="<%= @item.attributes[field[:id].to_sym] || field[:default] %>"
5
+ value="<%= @item.field_value_or_default(field) %>"
6
6
  >
7
7
  <% for option in field[:options] %>
8
8
  <option
@@ -12,7 +12,7 @@
12
12
  selected
13
13
  <% end %>
14
14
  <% else %>
15
- <% if option[:value].to_s == (@item.attributes[field[:id].to_sym] || field[:default]).to_s %>
15
+ <% if option[:value].to_s == @item.field_value_or_default(field) %>
16
16
  selected
17
17
  <% end %>
18
18
  <% end %>