Skip to content

Commit

Permalink
Creates a dontpad-like app
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusRich authored and alaxalves committed Apr 6, 2018
1 parent e1ad6cd commit 06cab03
Show file tree
Hide file tree
Showing 25 changed files with 405 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .rake_tasks~
@@ -0,0 +1,42 @@
about
app:template
app:update
assets:clean[keep]
assets:clobber
assets:environment
assets:precompile
cache_digests:dependencies
cache_digests:nested_dependencies
db:create
db:drop
db:environment:set
db:fixtures:load
db:migrate
db:migrate:status
db:rollback
db:schema:cache:clear
db:schema:cache:dump
db:schema:dump
db:schema:load
db:seed
db:setup
db:structure:dump
db:structure:load
db:version
dev:cache
initializers
log:clear
middleware
notes
notes:custom
restart
routes
secret
stats
test
test:db
test:system
time:zones[country_or_offset]
tmp:clear
tmp:create
yarn:install
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -30,6 +30,8 @@ gem 'jbuilder', '~> 2.5'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

gem 'jquery-rails'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -72,6 +72,10 @@ GEM
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -179,6 +183,7 @@ DEPENDENCIES
capybara (~> 2.13)
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 3.7)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -13,3 +13,6 @@
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require jquery3
//= require jquery_ujs
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/pages.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/pages.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the Pages controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
84 changes: 84 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
46 changes: 46 additions & 0 deletions app/controllers/pages_controller.rb
@@ -0,0 +1,46 @@
class PagesController < ApplicationController
before_action :set_page, only: [:find, :show, :update]

def find
if(@page.nil?)
@page = Page.new(page_params)
@page.save
end

render "show"
end


# RAILS STUFF

# GET /pages/1
# GET /pages/1.json
def show
end

# PATCH/PUT /pages/1
# PATCH/PUT /pages/1.json
def update
# puts "ENTROU NO LUGAR"
respond_to do |format|
if @page.update(page_params)
format.html { }
format.json { }
else
# format.html { render :edit }
# format.json { render json: @page.errors, status: :unprocessable_entity }
end
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_page
@page = Page.find_by(url: params[:url])
end

# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.permit(:content, :url)
end
end
2 changes: 2 additions & 0 deletions app/helpers/pages_helper.rb
@@ -0,0 +1,2 @@
module PagesHelper
end
2 changes: 2 additions & 0 deletions app/models/page.rb
@@ -0,0 +1,2 @@
class Page < ApplicationRecord
end
27 changes: 27 additions & 0 deletions app/views/pages/_form.html.erb
@@ -0,0 +1,27 @@
<%= form_with(model: page, local: true) do |form| %>
<% if page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(page.errors.count, "error") %> prohibited this page from being saved:</h2>

<ul>
<% page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :content %>
<%= form.text_area :content, id: :page_content %>
</div>

<div class="field">
<%= form.label :url %>
<%= form.text_field :url, id: :page_url %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/pages/_page.json.jbuilder
@@ -0,0 +1,2 @@
json.extract! page, :id, :content, :url, :created_at, :updated_at
json.url page_url(page, format: :json)
6 changes: 6 additions & 0 deletions app/views/pages/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing Page</h1>

<%= render 'form', page: @page %>
<%= link_to 'Show', @page %> |
<%= link_to 'Back', pages_path %>
29 changes: 29 additions & 0 deletions app/views/pages/index.html.erb
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Pages</h1>

<table>
<thead>
<tr>
<th>Content</th>
<th>Url</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @pages.each do |page| %>
<tr>
<td><%= page.content %></td>
<td><%= page.url %></td>
<td><%= link_to 'Show', page %></td>
<td><%= link_to 'Edit', edit_page_path(page) %></td>
<td><%= link_to 'Destroy', page, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Page', new_page_path %>
1 change: 1 addition & 0 deletions app/views/pages/index.json.jbuilder
@@ -0,0 +1 @@
json.array! @pages, partial: 'pages/page', as: :page
5 changes: 5 additions & 0 deletions app/views/pages/new.html.erb
@@ -0,0 +1,5 @@
<h1>New Page</h1>

<%= render 'form', page: @page %>
<%= link_to 'Back', pages_path %>
30 changes: 30 additions & 0 deletions app/views/pages/show.html.erb
@@ -0,0 +1,30 @@
<h1>Dontfile</h1>
<h4>Though it's just a dontpad for now...</h4>

<textarea id="content" style="width: 100%; min-height: 400px;" oninput="save()"
data-path="<%= @page.url %>"
>
<%= @page.content %>
</textarea>

<script>
function save() {
var element = document.getElementById("content");
var path = "/" + element.getAttribute("data-path");
var value = element.value;

console.log(path);

$.ajax({
url: path,
type: "PATCH",
data: {
url: path,
content: value
},
success: function(resp) {
console.log("foi carai");
}
})
}
</script>
1 change: 1 addition & 0 deletions app/views/pages/show.json.jbuilder
@@ -0,0 +1 @@
json.partial! "pages/page", page: @page
4 changes: 4 additions & 0 deletions config/routes.rb
@@ -1,3 +1,7 @@
Rails.application.routes.draw do
# resources :pages

get '/:url', to: 'pages#find'
patch '/:url', to: 'pages#update'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
10 changes: 10 additions & 0 deletions db/migrate/20180406161044_create_pages.rb
@@ -0,0 +1,10 @@
class CreatePages < ActiveRecord::Migration[5.1]
def change
create_table :pages do |t|
t.text :content
t.string :url

t.timestamps
end
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
@@ -0,0 +1,25 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180406161044) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "pages", force: :cascade do |t|
t.text "content"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end

0 comments on commit 06cab03

Please sign in to comment.