Skip to content

Commit

Permalink
Merge pull request #38 from spartansystems/model_scaffolding
Browse files Browse the repository at this point in the history
Model scaffolding
  • Loading branch information
samcdavid committed Oct 30, 2015
2 parents a73bcc6 + 32c192c commit c51edfa
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 228 deletions.
269 changes: 51 additions & 218 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,50 @@ on line 122 of the Active Admin initializer.

Now you shall run the migration and seeds with:
```ruby
bundle exec rake db:setup
bundle exec rake db:create
bundle exec rake db:migrate
```
To verify, start the server and visit `localhost:3000/admin`. If you can login
as `admin@example.com` with the password `password` you have successfully
completed this step.

### Atomic CMS
#### Routes
Update your `config/routes.rb` to include the following:
```ruby
mount AtomicCms::Engine => "/atomic_cms", as: :atomic_cms
# If you are going to use a catch all route add the following line
get "*path", to: "pages#show", controller: "pages", as: :page, format: false
root to: 'pages#show', controller: "pages"
```
The last two lines need to be at the **END** of your `routes.rb` file.

##### Devise Authentication
Change the mount point above to be the following:
```ruby
authenticate :admin_user, -> (u) { u.admin? } do
mount AtomicCms::Engine => "/atomic_cms", as: :atomic_cms
end
```
where `u` is the user and `admin?` the authentication method you have on that
user; it should return a boolean value.

#### Scaffold Generator
Execute the following to create a model for your static pages:
```ruby
rails g atomic_cms:scaffold page
```
*After this you should run your migrations.*

Here is a list of everything that is generated for you:

* Model
* Controller
* Active Admin Form
* Migration **Will need to be edited**
* Show View
* Route

### Media Upload
To install the media tables so that you can upload files until your heart is
literally full run:
Expand Down Expand Up @@ -104,234 +142,29 @@ this is properly imported:
//
// .status_tag { background: #6090DB; }
```
*NOTE:* At the minimum, here is what is needed in `active_admin.scss`:
```scss
@import "active_admin/mixins";
@import "active_admin/base";
@import "atomic_cms";

### Atomic CMS
#### Routes
Update your `config/routes.rb` to include the following:
```ruby
mount AtomicCms::Engine => "/atomic_cms", as: :atomic_cms
get "*path", to: "pages#show", controller: "pages", as: :page, format: false
root to: 'pages#show', controller: "pages"
```
The last two lines need to be at the **END** of your `routes.rb` file.

##### Devise Authentication
Change the mount point above to be the following:
```ruby
authenticate :admin_user, -> (u) { u.admin? } do
mount AtomicCms::Engine => "/atomic_cms", as: :atomic_cms
end
```
where `u` is the user and `admin?` the authentication method you have on that
user; it should return a boolean value.

#### Model
Execute the following to create a model for your static pages:
```ruby
rails g model pages title:string path:string content:text
```
After this you should run your migrations.

Update your Page model to match the following:
```ruby
class Page < ActiveRecord::Base
include AtomicCms::HasComponents
validates :title, :path, presence: true, uniqueness: true
component_attr :content
end
```
#### Controller
Create a controller by running the following:
```ruby
rails g controller pages
#component_preview {
// Include application specific styling here
}
```
Update your PagesController to match the following:
```ruby
require 'json'

class PagesController < ApplicationController
def show
@page = Page.find_by_path(request.path)
if @page
render cms_template
else
render file: "#{Rails.root}/public/404.html", status: :not_found
Rails.logger.error "404 - Page #{request.path} cannot be found."
end
end

private

def cms_template
File.join("pages", "page")
end
end
```
#### Views
Create a view at `app/views/pages/page.html.slim` that contains the following:
```ruby
= @page.content_object.render
```
#### Components
Create the following component view at
`app/views/components/text_block.html.slim`:
```slim
.wrapper.large-margin
.text-block
.content-text
- if add_option(options[:header])
h2
= options[:header]
= markdown(options[:content])
Generate components by executing the following:
```
Create the following component class at `app/components/text_block_component.rb`:
```ruby
class TextBlockComponent < AtomicAssets::Component
def edit
rtn = cms_fields(field_types)
rtn << h.component(:text_block, field_previews).render
rtn.html_safe
end

protected

def field_previews
{
header: "{{preview.header}}",
content: markdown_preview('preview.content')
}
end

def field_types
{
header: { field_type: "text" },
content: { field_type: "markdown" }
}
end
end
rails g atomic_assets:component text_block
```
Add the following to `app/assets/stylesheets/components/_text_block.scss`:
```scss
.text-block{
position: relative;
.content-text{
ul{
margin-left: 10px;
padding-left: 0px;
position: relative;
display: inline-block;
li{
list-style: disc;
margin-left: 20px;
}
}
}
&:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
}
```
### More Active Admin
In order to add components to a page, create
`app/views/admin/_edit_buttons.html.slim` and make it match the following:
In order for components to be utilized when managing content, a partial needs to
be created similar to the following at `app/views/admin/_edit_buttons.html.slim`:
```slim
ol.edit-buttons
li
= link_to 'Text Block', atomic_cms.edit_component_path('text_block'), class: 'button'
```
Create an admin page for the Pages model at `app/admin/page.rb` and make it
match the following:
```ruby
ActiveAdmin.register Page do
permit_params :title, :path, :content

index do
selectable_column
column :path
column :title
column :created_at
column :updated_at
actions
end

form html: { class: "edit-atomic-content" } do |f|
f.semantic_errors(*f.object.errors.keys)

# new form
if !f.object.persisted?
f.inputs "Page Details" do
f.input :title
f.input :path
end
f.actions

# edit form
else
div class: "buttons" do
render partial: "admin/edit_buttons"
f.actions
end

columns do
column span: 3 do
panel "Draft", id: "draft-panel" do
render partial: "components/edit", locals: { f: f }
end
end

column id: "edit-node-column" do
div id: "edit-page" do
f.inputs "Page Details" do
f.input :cms_type, type: :hidden,
input_html: { value: "page", id: "cms_type" }
f.input :title
f.input :path
end
end

div id: "edit-node" do
f.inputs "Edit Element" do
div id: "edit-node-fields"
end

f.actions do
li class: "move" do
a "Up", "#", class: "button", id: "move-node-up"
end
li class: "move" do
a "Down", "#", class: "button", id: "move-node-down"
end
li class: "cancel" do
a "Done", "#", class: "button", id: "done-edit-node"
end
li class: "delete" do
a "Delete", "#", class: "button", id: "delete-node"
end
end
end
end
end
end
end

show do
div id: "component_preview" do
div page.content_render
end
end

controller do
# permit all params until we can whitelist content_object
def permitted_params
params.permit!
end
end
end
```
#### Config
Update `config/application.rb` to include:
```ruby
Expand Down
5 changes: 2 additions & 3 deletions atomic_cms.gemspec
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
Gem::Specification.new do |s|
s.name = 'atomic_cms'
s.version = '0.2.1'
s.version = '0.2.2'
s.date = '2015-06-19'
s.summary = 'Atomic CMS'
s.description = 'Live CMS powered by atomic assets.'
s.authors = ['Don Humphreys']
s.email = 'dhumphreys88@gmail.com'
s.files = `git ls-files lib`.split(/\n/)
s.files = `git ls-files`.split(/\n/)
s.test_files = Dir['spec/**/*']
# s.homepage = 'http://rubygems.org/gems/atomic_cms'
# s.license = 'MIT'

s.add_dependency 'rails', '~> 4.2'
s.add_dependency 'activeadmin', '1.0.0.pre2'
s.add_dependency 'angularjs-rails', '~> 1.3', '< 1.4'
s.add_dependency 'atomic_assets', '~> 0.1.0'
s.add_dependency 'jquery-rails', '~> 4.0', '>= 4.0.3'
s.add_dependency 'redcarpet', '~> 3.3'
Expand Down
40 changes: 33 additions & 7 deletions lib/generators/atomic_cms/assets/assets_generator.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
module AtomicCms
module Generators
class AssetsGenerator < Rails::Generators::Base
def install_bourbon
gem "bourbon", "~> 4.2.6"
end

def install_neat
gem "neat", "~> 1.7.2"
end

def install_bitters
gem "bitters", "~> 1.1.0"
run "bitters install --path ./app/assets/stylesheets"
end

def install_angular
gem "angularjs-rails", "~> 1.3", "< 1.4"
end

def initialize_active_admin_javascript
javascript_asset = 'app/assets/javascripts/active_admin.js.coffee'
javascript_asset = "app/assets/javascripts/active_admin.js.coffee"
entries = [
'#= require angular',
'#= require angular-sanitize',
'#= require atomic_cms'
"#= require angular",
"#= require angular-sanitize",
"#= require atomic_cms"
]
append_to_file( asset_file: javascript_asset, entries: entries )
end

def initialize_active_admin_scss
scss_asset = 'app/assets/stylesheets/active_admin.scss'
scss_asset = "app/assets/stylesheets/active_admin.scss"
entries = [
'@import "atomic_cms"',
'@import "bourbon";',
'@import "neat";',
'@import "base/variables";',
'@import "base/grid-settings";',
'@import "atomic_cms";',
'',
'#component_preview {',
' @import "base/buttons";',
' @import "base/forms";',
' @import "base/lists";',
' @import "base/tables";',
' @import "base/typography";',
' // When editing a page through Atomic CMS',
' // images with broken links should not be displayed.',
' img[src="image"] { display:none !important; }',
Expand All @@ -28,7 +54,7 @@ def initialize_active_admin_scss
private

def append_to_file(options)
open(options[:asset_file], 'a') do |asset_file|
open(options[:asset_file], "a") do |asset_file|
options[:entries].each do |entry|
asset_file.puts entry
end
Expand Down
Loading

0 comments on commit c51edfa

Please sign in to comment.