[deploy] Refactor: Remove Block Model and All Related Code (#9370)

This commit is contained in:
Molly Struve 2020-07-17 15:32:39 -05:00 committed by GitHub
parent b564592a4e
commit 62d43a1ce7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 0 additions and 386 deletions

View file

@ -1,80 +0,0 @@
class BlocksController < ApplicationController
before_action :set_block, only: %i[show edit update destroy]
after_action :verify_authorized
# GET /blocks
# GET /blocks.json
def index
authorize Block
@blocks = Block.order("index_position ASC")
end
# GET /blocks/1
# GET /blocks/1.json
def show
authorize @block
end
# GET /blocks/new
def new
authorize Block
@block = Block.new
end
# GET /blocks/1/edit
def edit
authorize @block
end
# POST /blocks
# POST /blocks.json
def create
authorize Block
@block = Block.new(permitted_attributes(Block))
@block.user_id = current_user.id
respond_to do |format|
if @block.save
format.html { redirect_to @block, notice: "Block was successfully created." }
format.json { render :show, status: :created, location: @block }
else
format.html { render :new }
format.json { render json: @block.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /blocks/1
# PATCH/PUT /blocks/1.json
def update
authorize @block
respond_to do |format|
if @block.update(permitted_attributes(@block))
@block.publish! if permitted_attributes(@block)[:publish_now]
format.html { redirect_to @block, notice: "Block was successfully updated." }
format.json { render :show, status: :ok, location: @block }
else
format.html { render :edit }
format.json { render json: @block.errors, status: :unprocessable_entity }
end
end
end
# DELETE /blocks/1
# DELETE /blocks/1.json
def destroy
authorize @block
@block.destroy
respond_to do |format|
format.html { redirect_to blocks_url, notice: "Block was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_block
@block = Block.find(params[:id])
end
end

View file

@ -1,41 +0,0 @@
class Block < ApplicationRecord
attr_accessor :publish_now
belongs_to :user
validate :permissions
before_save :process_html
before_save :process_javascript
before_save :process_css
def publish!
self.published_html = processed_html
self.published_javascript = processed_javascript
self.published_css = processed_css
self.featured = true
save
end
private
def process_html
self.processed_html = input_html
end
def process_javascript
self.processed_javascript = input_javascript
end
def process_css
scoped_scss = ".block-wrapper-#{id} { #{input_css}}"
se = Sass::Engine.new(scoped_scss, syntax: :scss)
self.processed_css = se.render
end
def permissions
return if user&.has_role?(:super_admin)
errors.add(:commentable_id, "is not valid.")
end
end

View file

@ -1,33 +0,0 @@
class BlockPolicy < ApplicationPolicy
def index?
user_admin?
end
def show?
user_admin?
end
def new?
user_admin?
end
def edit?
user_admin?
end
def create?
user_admin?
end
def update?
user_admin?
end
def destroy?
user_admin?
end
def permitted_attributes
%i[input_html input_css input_javascript featured index_position publish_now]
end
end

View file

@ -1,9 +0,0 @@
<div style="font-size:23px;padding:10px 0px">
Featured: <%= block.featured %> | Position: <%= block.index_position %>
<% if block.processed_html != block.published_html || block.processed_css != block.published_css %>
<%= form_for(block) do |f| %>
<%= f.hidden_field :publish_now %><br />
<button style="cursor:pointer;background:#30cb98;color:white;border:0px;padding:10px;font-size:25px;">PUBLISH</button>
<% end %>
<% end %>
</div>

View file

@ -1,88 +0,0 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/codemirror.min.js"
integrity="sha256-UQey9E6ZZXhvoLNXvVmU5lRSbOcCZe707EBLJTtMwSc=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/codemirror.min.css"
integrity="sha256-GGAcbnLrt4/AHq5cP2+2UWetYcNsQNjNMqSRStBoPLQ=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/mode/css/css.min.js"
integrity="sha256-FM1AMdIH6Zrnz+JGuB5uTB6jxAJ8/XvGvq4HJM0YtDQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/mode/javascript/javascript.min.js"
integrity="sha256-Zci/fLjT1ALmFze3qSOXnHkYOOpyj5WUHM/Ioy+Bpyo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.26.0/mode/htmlmixed/htmlmixed.min.js"
integrity="sha256-fYwhT0v38tXMnxU+JibUW1drchatADDwdPWvOuxI/yg=" crossorigin="anonymous"></script>
<script>
setTimeout(function () {
var myTextArea = document.getElementById("block_input_css");
var myCodeMirror = CodeMirror.fromTextArea(myTextArea);
var myTextArea = document.getElementById("block_input_javascript");
var myCodeMirror = CodeMirror.fromTextArea(myTextArea);
var myTextArea = document.getElementById("block_input_html");
var myCodeMirror = CodeMirror.fromTextArea(myTextArea);
// var myCodeMirror = CodeMirror(function(elt) {
// myTextArea.parentNode.replaceChild(elt, myTextArea);
// }, {
// value: myTextArea.value,
// mode: "css"
// });
}, 1)
</script>
<style>
.form-wrapper form {
width: 800px;
max-width: 100%;
margin: auto;
text-align: left !important;
font-size: 1.5em;
}
textarea {
opacity: 0;
}
pre {
overflow: visible !important;
}
.CodeMirror {
border: 2px solid red;
margin-top: 10px;
font-size: 14px;
padding: 2px;
}
</style>
<div class="form-wrapper">
<%= form_for(@block) do |f| %>
<% if @block.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@block.errors.count, "error") %> prohibited this block from being saved:</h2>
<ul>
<% @block.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<style>
textarea {
height: 250px;
width: 600px;
max-width: 90%;
}
</style>
<%= f.label :input_css %><br />
<%= f.text_area :input_css %><br />
<%= f.label :input_javascript %><br />
<%= f.text_area :input_javascript %><br />
<%= f.label :input_html %><br />
<%= f.text_area :input_html %><br />
<%= f.label :featured %><br />
<%= f.check_box :featured %><br />
<%= f.label :index_position %><br />
<%= f.text_field :index_position %><br />
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>

View file

@ -1,2 +0,0 @@
<br /><br /><br /><br />
<%= render "form" %>

View file

@ -1,14 +0,0 @@
<div class="home" id="index-container">
<div class="articles-list">
<% @blocks.each_with_index do |block, i| %>
<%= render "articles/block", block: block, show_published: false, i: i %>
<center>
<%= render "blocks/actions", block: block %>
<%= link_to "Edit", edit_block_path(block), style: "color:white;display:block;font-size:35px;background:rgb(0, 93, 190);padding:10px 0px;" %>
</center>
<% end %>
</div>
</div>
<%= link_to "New Block", new_block_path %>

View file

@ -1,4 +0,0 @@
json.array!(@blocks) do |block|
json.extract! block, :id
json.url block_url(block, format: :json)
end

View file

@ -1,5 +0,0 @@
<h1>New Block</h1>
<%= render "form" %>
<%= link_to "Back", blocks_path %>

View file

@ -1,10 +0,0 @@
<div class="home" id="index-container">
<div class="articles-list">
<%= render "articles/block", block: @block, show_published: false, i: 0 %>
<center>
<%= render "blocks/actions", block: @block %>
<%= link_to "Edit", edit_block_path(@block), style: "color:white;display:block;font-size:35px;background:rgb(0, 93, 190);padding:10px 0px;" %>
</center>
<h1><a href="/blocks" style="color:blue">Go to Index</a></h1>
</div>
</div>

View file

@ -1 +0,0 @@
json.extract! @block, :id, :created_at, :updated_at

View file

@ -203,7 +203,6 @@ Rails.application.routes.draw do
end
end
resources :image_uploads, only: [:create]
resources :blocks
resources :notifications, only: [:index]
resources :tags, only: [:index] do
collection do

View file

@ -1,20 +0,0 @@
require "rails_helper"
RSpec.describe Block, type: :model do
let_it_be(:user) { create(:user) }
let_it_be(:block) { described_class.new(user: user, input_html: "hello") }
it "creates processed_html after published!" do
user.add_role(:super_admin)
block.publish!
expect(block.processed_html).to eq("hello")
end
it "is not valid without user" do
expect(described_class.new(user: nil)).not_to be_valid
end
it "is not valid with non-admin user" do
expect(block).not_to be_valid
end
end

View file

@ -1,30 +0,0 @@
require "rails_helper"
RSpec.describe BlockPolicy, type: :policy do
subject { described_class.new(user, block) }
let(:block) { build_stubbed(:block) }
let(:valid_attributes) do
%i[input_html input_css input_javascript featured index_position publish_now]
end
context "when not signed in" do
let(:user) { nil }
it { within_block_is_expected.to raise_error(Pundit::NotAuthorizedError) }
end
context "when signed in as a regular user" do
let(:user) { build_stubbed(:user) }
it { is_expected.to forbid_actions(%i[index show new edit create update destroy]) }
end
context "when user is signed in as a super admin" do
let(:user) { build(:user, :super_admin) }
it { is_expected.to permit_actions(%i[index show new edit create update destroy]) }
it { is_expected.to permit_mass_assignment_of(valid_attributes) }
end
end

View file

@ -1,48 +0,0 @@
require "rails_helper"
RSpec.describe "Blocks", type: :request do
let(:user) { create(:user, :super_admin) }
before { sign_in user }
describe "GET blocks index" do
it "renders proper blocks index" do
create(:block, user_id: user.id, input_css: ".blue { color: blue;}")
get "/blocks"
expect(response.body).to include("color: blue")
end
end
describe "POST blocks" do
it "creates block from input data" do
post "/blocks", params: {
block: {
input_css: ".blue { color: blue;}",
input_html: "yo",
input_javascript: "alert('hey')"
}
}
expect(Block.all.size).to eq(1)
end
end
describe "PUT blocks" do
it "updates block from input data" do
block = create(:block, user_id: user.id, input_css: ".blue { color: blue;}")
put "/blocks/#{block.id}", params: {
block: { input_css: ".blue { color: red;}",
input_html: "yo",
input_javascript: "alert('hey')" }
}
expect(Block.last.processed_css).to include("color: red")
end
end
describe "DELETE blocks" do
it "updates block from input data" do
block = create(:block, user_id: user.id, input_css: ".blue { color: blue;}")
delete "/blocks/#{block.id}"
expect(Block.all.size).to eq(0)
end
end
end