cm_page_builder-rails 0.1.9 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e374106c7415c51adba2cb3faa39cf38c434e62de0e88fa5395a6cd613dc884
4
- data.tar.gz: d83d6286bb377b691806311132d0dd62980aa95573b1d8bb5edc391abcf784bb
3
+ metadata.gz: 1e528443d6f1df388159a706b5bcab30e2cf42c06af6628ed763d398a202c263
4
+ data.tar.gz: 81fc89511291e3320123adfd21971d2064a1891b176b78ea59fed00f07b3c62c
5
5
  SHA512:
6
- metadata.gz: 606ade1535908f25bbdf80b3fadbcab70f45e0a3c628202a146a518e243642623a9539adf39b5170a34b77bbaa0a480df58cc6edec4aa4970e3b1d5008c4be58
7
- data.tar.gz: 25fc83b7d1b59e9665b44429f61e0cc13bbfde8c5d8da71fb573608aea2807e4ddb9d3c7c2ed5543b999bd0c8d2e0e5ebd22f2e27a88a5a4fb7d57ff4a4bef00
6
+ metadata.gz: f10ae957d38fa4ba023a078387cbf8dfdc796a16f810111a4eb17a5c3be71f3f6030beca16321f10b0cc2407eba9f2a24ddd76807b08bf2bc6b61266978ecba0
7
+ data.tar.gz: 3022134f1afda91c279f4e3c955ad486a6ab179f8ff62d3400948f857274b3078dd7eadef2a381b5ca519878643018528a16362063fdae5681317fa92dc9f2fc
data/README.md CHANGED
@@ -1,27 +1,47 @@
1
+ [![Gem Version](https://badge.fury.io/rb/cm_page_builder-rails.svg)](https://badge.fury.io/rb/cm_page_builder-rails)
2
+
1
3
  # CmPageBuilder::Rails
2
- This gem's purpose is to allow easy integration between the Commutatus react package cm-page-builder and any rails applications created by
4
+ This gem's purpose is to allow easy integration between the Commutatus javascript library [cm-page-builder](https://github.com/commutatus/cm-page-builder) and any Rails application that requires storing and editing content through Rails-rendered webpages.
3
5
 
4
- ## Usage
5
- To activate this module, add `include CmPageBuilder::Rails::HasCmContent` on top of any model file that should have an associated rich text field.
6
+ ## Requirements
7
+
8
+ * Rails 6+
9
+ * Webpacker must be enabled in the project
10
+ * S3 as the image backend storage
6
11
 
7
- ## Installation
8
12
 
13
+ ## Initial setup:
9
14
 
10
- ```ruby
11
- gem 'react-rails', '~> 2.6.0'
12
- gem 'cm_page_builder-rails'
13
- ```
15
+ * Add the following lines to the gemfile:
16
+ ```ruby
17
+ gem 'react-rails', '~> 2.6.0'
18
+ gem 'cm_page_builder-rails'
19
+ ```
20
+ And then execute:
21
+ ```bash
22
+ $ bundle
23
+ ```
24
+ * Follow all react-rails installation instructions (https://github.com/reactjs/react-rails)
25
+ *Important:* ensure that the `hello_world` example successfully works
14
26
 
15
- And then execute:
16
- ```bash
17
- $ bundle
18
- ```
27
+ * Install the javascript packages with:
28
+ ```bash
29
+ yarn add cm-page-builder@1.3.0 -E
30
+ yarn add babel-runtime@^6.26.0
31
+ ```
32
+ (the emoji-mart package, a dependency of cm-page-builder, sometimes bugs out during the webpack build process without the babel-runtime)
19
33
 
20
- Follow all react-rails installation instructions
34
+ * Run `rails cm_page_builder_rails:install:migrations`
21
35
 
22
- Then run `rails cm_page_builder_rails:install:migrations`
36
+ * Run `rails generate cm_page_builder:rails:install` to install the JSX component. This assumes your webpack folder is inside `app/javascripts`
37
+
38
+ * In `config/routes.rb`, mount the endpoint with the line `mount CmPageBuilder::Rails::Engine => "/cm_page_builder"`
39
+
40
+ ## Usage
41
+ This package comes with a concern, `CmPageBuilder::Rails::HasCmContent` `include CmPageBuilder::Rails::HasCmContent`
42
+ To activate this module, add `include CmPageBuilder::Rails::HasCmContent` on top of any model file that should have an associated page builder record.
23
43
 
24
- In `config/routes.rb`, mount the endpoint with the line `mount CmPageBuilder::Rails::Engine => "/cm_page_builder"`
44
+ The page builder record will be accessible from the model as *page* (`@model.page`). The show path is accessible through the route `cm_page_builder_rails.page_path(@page)`, and the edit path through the route `cm_page_builder_rails.edit_page_path(@page)`
25
45
 
26
46
  ### Setting up CORS for aws
27
47
  Do this or the direct upload capabilities won't work
@@ -29,7 +49,7 @@ https://keithpblog.org/post/active-storage-on-amazon-s3/
29
49
 
30
50
  ### Building the gem
31
51
  `gem build cm_page_builder-rails.gemspec`
32
- `gem push <generated gem name>`
52
+ `gem push cm_page_builder-rails-0.1.x.gem`
33
53
 
34
54
  ## Contributing
35
55
  Contribution directions go here.
@@ -5,11 +5,11 @@ module CmPageBuilder::Rails
5
5
 
6
6
  accepts_nested_attributes_for :page_components, allow_destroy: true
7
7
 
8
- def get_components
8
+ def components(**args)
9
9
  page_components.with_attached_component_attachment.select(:id, :uuid, :component_type, :position, :content).map do |component|
10
- json_component = component.as_json.transform_keys! {|key| key.camelize(:lower)}
10
+ json_component = component.as_json.transform_keys! { |key| key.camelize(:lower) }
11
11
  json_component["id"] = component[:uuid]
12
- json_component["component_attachment"] = _get_attachment(component)
12
+ json_component["component_attachment"] = _get_attachment(component, args[:max_width])
13
13
  json_component
14
14
  end
15
15
  end
@@ -33,37 +33,42 @@ module CmPageBuilder::Rails
33
33
  end
34
34
  end
35
35
 
36
- def _get_attachment(component)
37
- attachment = component.component_attachment.attachment
38
- if attachment
36
+ private
37
+
38
+ def _get_attachment(component, max_width=nil)
39
+ attachment = component.component_attachment.attachment
40
+ return unless attachment
41
+
42
+ blob = attachment.blob
39
43
  attachment_data = {
40
44
  filename: attachment.filename.to_s,
45
+ filesize: blob.byte_size,
41
46
  url: attachment.service_url
42
47
  }
43
- dimensions = attachment.blob.metadata
44
- if dimensions['width']
48
+ if blob.variable?
49
+ dimensions = blob.metadata
45
50
  dimensions['orientation'] =
46
51
  if dimensions['width'] > dimensions['height']
47
52
  'landscape'
48
53
  else
49
54
  'portrait'
50
55
  end
51
- attachment_data["dimensions"] = dimensions
56
+ attachment_data[:dimensions] = dimensions
57
+ attachment_data[:url] = attachment.variant(resize_to_limit: [max_width, nil]).processed.service_url if max_width
52
58
  end
53
59
  attachment_data
54
60
  end
55
- end
56
61
 
57
- def _save_component(component)
58
- page_component = page_components.find_or_initialize_by(uuid: component["id"])
59
- signed_id = component.dig("component_attachment", "signed_id")
60
- component_data = {
61
- content: component["content"],
62
- position: component["position"],
63
- component_type: component["componentType"]
64
- }
65
- component_data[:component_attachment] = signed_id if signed_id
66
- page_component.update!(component_data)
67
- end
62
+ def _save_component(component)
63
+ page_component = page_components.find_or_initialize_by(uuid: component["id"])
64
+ signed_id = component.dig("component_attachment", "signed_id")
65
+ component_data = {
66
+ content: component["content"],
67
+ position: component["position"],
68
+ component_type: component["componentType"]
69
+ }
70
+ component_data[:component_attachment] = signed_id if signed_id
71
+ page_component.update!(component_data)
72
+ end
68
73
  end
69
74
  end
@@ -8,7 +8,6 @@
8
8
 
9
9
  .field
10
10
  input type='hidden' id="content-editor" name="page[components]"
11
- - components = @page.get_components
12
- = react_component("cm_content_manager/Content", {input: "content-editor", components: components})
11
+ = react_component("cm_content_manager/Content", { input: "content-editor", components: @page.components, assetBaseUrl: ActiveStorage::Blob.service.bucket.url})
13
12
 
14
13
  .actions = f.submit
@@ -3,6 +3,3 @@ h1 Editing page
3
3
  == render 'form'
4
4
 
5
5
  => link_to 'Show', @page
6
- '|
7
- =< link_to 'Back', pages_path
8
-
@@ -1,8 +1,8 @@
1
1
 
2
2
 
3
-
4
-
5
-
6
3
  CmPageBuilder::Rails::Engine.routes.draw do
7
- resources :pages
4
+ # Only individual items can be shown and edited.
5
+ # The only way to access these is through their parent record in the main application, so no index
6
+ # Their lifecycle is also associated with their parent record's, so no create and destroy
7
+ resources :pages, only: [:show, :edit, :update]
8
8
  end
@@ -1,5 +1,5 @@
1
1
  module CmPageBuilder
2
2
  module Rails
3
- VERSION = '0.1.9'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CmPageBuilder::Rails
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Copy the jsx component"
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def copy_jsx_component
10
+ template "app/javascripts/components/cm_content_manager/Content.jsx"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ import React from "react"
2
+ import PropTypes from "prop-types"
3
+ import PageBuilder from 'cm-page-builder'
4
+
5
+ class Content extends React.Component {
6
+ constructor(props) {
7
+ super(props)
8
+ }
9
+
10
+
11
+ _updatePageComponent = (id, data, type, key) => {}
12
+
13
+
14
+ render () {
15
+ return (
16
+ <React.Fragment>
17
+ <PageBuilder
18
+ pageComponents={this.props.components}
19
+ handleUpdate={this._updatePageComponent}
20
+ updateComponentData={(data) =>
21
+ { $(`#${this.props.input}`).val(JSON.stringify(data))}
22
+ }
23
+ showTitle={false}
24
+ showEmoji={false}
25
+ showPageInfo={false}
26
+ useDirectStorageUpload={true}
27
+ assetBaseUrl={ this.props.assetBaseUrl }
28
+ meta={ {id: "debug"} }
29
+ status="Edit"
30
+ />
31
+ </React.Fragment>
32
+ );
33
+ }
34
+ }
35
+
36
+ Content.propTypes = {
37
+ input: PropTypes.string,
38
+ components: PropTypes.array,
39
+ assetBaseUrl: PropTypes.string
40
+ };
41
+ export default Content
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cm_page_builder-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Camilo Ernesto Forero Junco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-14 00:00:00.000000000 Z
11
+ date: 2020-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -82,7 +82,6 @@ files:
82
82
  - app/assets/stylesheets/cm_page_builder/rails/pages.css
83
83
  - app/assets/stylesheets/scaffold.css
84
84
  - app/controllers/cm_page_builder/rails/application_controller.rb
85
- - app/controllers/cm_page_builder/rails/page_components_controller.rb
86
85
  - app/controllers/cm_page_builder/rails/pages_controller.rb
87
86
  - app/helpers/cm_page_builder/rails/application_helper.rb
88
87
  - app/helpers/cm_page_builder/rails/page_components_helper.rb
@@ -93,15 +92,8 @@ files:
93
92
  - app/models/cm_page_builder/rails/page.rb
94
93
  - app/models/cm_page_builder/rails/page_component.rb
95
94
  - app/models/concerns/cm_page_builder/rails/has_cm_content.rb
96
- - app/views/cm_page_builder/rails/page_components/_form.html.slim
97
- - app/views/cm_page_builder/rails/page_components/edit.html.slim
98
- - app/views/cm_page_builder/rails/page_components/index.html.slim
99
- - app/views/cm_page_builder/rails/page_components/new.html.slim
100
- - app/views/cm_page_builder/rails/page_components/show.html.slim
101
95
  - app/views/cm_page_builder/rails/pages/_form.html.slim
102
96
  - app/views/cm_page_builder/rails/pages/edit.html.slim
103
- - app/views/cm_page_builder/rails/pages/index.html.slim
104
- - app/views/cm_page_builder/rails/pages/new.html.slim
105
97
  - app/views/cm_page_builder/rails/pages/show.html.slim
106
98
  - app/views/layouts/cm_page_builder/rails/application.html.erb
107
99
  - config/routes.rb
@@ -112,6 +104,8 @@ files:
112
104
  - lib/cm_page_builder/rails/application_helper.rb
113
105
  - lib/cm_page_builder/rails/engine.rb
114
106
  - lib/cm_page_builder/rails/version.rb
107
+ - lib/generators/cm_page_builder/rails/install_generator.rb
108
+ - lib/generators/cm_page_builder/rails/templates/app/javascripts/components/cm_content_manager/Content.jsx
115
109
  - lib/tasks/cm_page_builder/rails_tasks.rake
116
110
  - spec/dummy/Rakefile
117
111
  - spec/dummy/app/assets/config/manifest.js
@@ -1,62 +0,0 @@
1
- require_dependency "cm_page_builder/rails/application_controller"
2
-
3
- module CmPageBuilder::Rails
4
- class PageComponentsController < ApplicationController
5
- before_action :set_page_component, only: [:show, :edit, :update, :destroy]
6
-
7
- # GET /page_components
8
- def index
9
- @page_components = PageComponent.all
10
- end
11
-
12
- # GET /page_components/1
13
- def show
14
- end
15
-
16
- # GET /page_components/new
17
- def new
18
- @page_component = PageComponent.new
19
- end
20
-
21
- # GET /page_components/1/edit
22
- def edit
23
- end
24
-
25
- # POST /page_components
26
- def create
27
- @page_component = PageComponent.new(page_component_params)
28
-
29
- if @page_component.save
30
- redirect_to @page_component, notice: 'Page component was successfully created.'
31
- else
32
- render :new
33
- end
34
- end
35
-
36
- # PATCH/PUT /page_components/1
37
- def update
38
- if @page_component.update(page_component_params)
39
- redirect_to @page_component, notice: 'Page component was successfully updated.'
40
- else
41
- render :edit
42
- end
43
- end
44
-
45
- # DELETE /page_components/1
46
- def destroy
47
- @page_component.destroy
48
- redirect_to page_components_url, notice: 'Page component was successfully destroyed.'
49
- end
50
-
51
- private
52
- # Use callbacks to share common setup or constraints between actions.
53
- def set_page_component
54
- @page_component = PageComponent.find(params[:id])
55
- end
56
-
57
- # Only allow a trusted parameter "white list" through.
58
- def page_component_params
59
- params.require(:page_component).permit(:page_id, :content, :position, :component_type, :id)
60
- end
61
- end
62
- end
@@ -1,24 +0,0 @@
1
- = form_for @page_component do |f|
2
- - if @page_component.errors.any?
3
- #error_explanation
4
- h2 = "#{pluralize(@page_component.errors.count, "error")} prohibited this page_component from being saved:"
5
- ul
6
- - @page_component.errors.full_messages.each do |message|
7
- li = message
8
-
9
- .field
10
- = f.label :page
11
- = f.text_field :page
12
- .field
13
- = f.label :content
14
- = f.text_field :content
15
- .field
16
- = f.label :position
17
- = f.number_field :position
18
- .field
19
- = f.label :component_type
20
- = f.text_field :component_type
21
- .field
22
- = f.label :id
23
- = f.text_field :id
24
- .actions = f.submit
@@ -1,8 +0,0 @@
1
- h1 Editing page_component
2
-
3
- == render 'form'
4
-
5
- => link_to 'Show', @page_component
6
- '|
7
- =< link_to 'Back', page_components_path
8
-
@@ -1,29 +0,0 @@
1
- h1 Listing page_components
2
-
3
- table
4
- thead
5
- tr
6
- th Page
7
- th Content
8
- th Position
9
- th Component type
10
- th Id
11
- th
12
- th
13
- th
14
-
15
- tbody
16
- - @page_components.each do |page_component|
17
- tr
18
- td = page_component.page
19
- td = page_component.content
20
- td = page_component.position
21
- td = page_component.component_type
22
- td = page_component.id
23
- td = link_to 'Show', page_component
24
- td = link_to 'Edit', edit_page_component_path(page_component)
25
- td = link_to 'Destroy', page_component, data: { confirm: 'Are you sure?' }, method: :delete
26
-
27
- br
28
-
29
- = link_to 'New Page component', new_page_component_path
@@ -1,5 +0,0 @@
1
- h1 New page_component
2
-
3
- == render 'form'
4
-
5
- = link_to 'Back', page_components_path
@@ -1,21 +0,0 @@
1
- p#notice = notice
2
-
3
- p
4
- strong Page:
5
- = @page_component.page
6
- p
7
- strong Content:
8
- = @page_component.content
9
- p
10
- strong Position:
11
- = @page_component.position
12
- p
13
- strong Component type:
14
- = @page_component.component_type
15
- p
16
- strong Id:
17
- = @page_component.id
18
-
19
- => link_to 'Edit', edit_page_component_path(@page_component)
20
- '|
21
- =< link_to 'Back', page_components_path
@@ -1,21 +0,0 @@
1
- h1 Listing pages
2
-
3
- table
4
- thead
5
- tr
6
- th Container
7
- th
8
- th
9
- th
10
-
11
- tbody
12
- - @pages.each do |page|
13
- tr
14
- td = page.container
15
- td = link_to 'Show', page
16
- td = link_to 'Edit', edit_page_path(page)
17
- td = link_to 'Destroy', page, data: { confirm: 'Are you sure?' }, method: :delete
18
-
19
- br
20
-
21
- = link_to 'New Page', new_page_path
@@ -1,5 +0,0 @@
1
- h1 New page
2
-
3
- == render 'form'
4
-
5
- = link_to 'Back', pages_path