Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolating Admin Code #51

Merged
merged 9 commits into from
Sep 25, 2014
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ spec/dummy/.sass-cache
test_apps/**/db/migrate/**/*.rb
test_apps/**/db/*
test_apps/**/spec/**/*.rb
test_apps/**/app/upmin/**/*.rb
21 changes: 13 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ end

task :default => "spec:all"

def update_files
# Drop and reload spec files
sh "rm -rf spec/"
sh "cp -R ../../spec spec"
sh "cp ../../.rspec .rspec"

# Drop and reload Upmin::Model files
sh "rm -rf app/upmin/"
sh "cp -R ../../test_app_upmin app/upmin"
end

namespace :spec do
# Full bundle install & test.
%w(active_record_32 active_record_40 active_record_41 active_record_42 will_paginate).each do |gemfile|
Expand All @@ -33,10 +44,7 @@ namespace :spec do

sh "RAILS_ENV=test bundle exec rake db:drop db:create db:migrate --quiet"

# Drop and reload spec files
sh "rm -rf spec/"
sh "cp -R ../../spec spec"
sh "cp ../../.rspec .rspec"
update_files

# Run tests
sh "bundle exec rake"
Expand All @@ -50,10 +58,7 @@ namespace :spec do
Dir.chdir("test_apps/#{gemfile}")
puts "Re-testing in #{`pwd`}. Bundle install and migration updates will NOT happen!"

# Drop and reload spec files
sh "rm -rf spec/"
sh "cp -R ../../spec spec"
sh "cp ../../.rspec .rspec"
update_files

# Run tests
sh "bundle exec rake"
Expand Down
1 change: 0 additions & 1 deletion app/assets/javascripts/upmin/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//= require jquery
//= require jquery_ujs
//= require ./jquery-clockpicker
//= require ./moment
//= require ./pikaday
//= require ./helpers
//= require_tree .
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/upmin/base.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ body {
}



.wizard span {padding: 12px 12px 10px 12px; margin-right:5px; background:#efefef; position:relative; display:inline-block; }
.wizard span:before {width:0px; height:0px; border-top: 20px inset transparent; border-bottom: 20px inset transparent; border-left: 20px solid #fff; position: absolute; content: ""; top: 0; left: 0;}
.wizard span:after {width:0px; height:0px; border-top: 20px inset transparent; border-bottom: 20px inset transparent; border-left: 20px solid #efefef; position: absolute; content: ""; top: 0; right: -20px; z-index:2;}
Expand Down
12 changes: 11 additions & 1 deletion app/assets/stylesheets/upmin/instances.css.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
@import "colors";

.search-result-link {
.upmin-model {
margin-bottom: 10px;
}
}

.upmin-model {
padding: 20px;
margin: 10px 0px 10px 0;
margin: 10px 0px 250px 0;
border: 1px solid #eee;
border-left-width: 5px;
border-radius: 3px;
Expand Down Expand Up @@ -65,6 +71,10 @@
padding: 9px;
margin-bottom: 0;
min-height: 40px;

&.action-well {
margin-bottom: 32px;
}
}

a.active-tag-link {
Expand Down
80 changes: 37 additions & 43 deletions app/controllers/upmin/models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class ModelsController < ApplicationController
before_filter :set_model, only: [:show, :update, :action]

before_filter :set_page, only: [:search]
before_filter :set_query, only: [:search]

before_filter :set_method, only: [:action]
before_filter :set_action, only: [:action]
before_filter :set_arguments, only: [:action]

def dashboard
Expand All @@ -25,34 +26,28 @@ def new
# POST /:model_name
def create
@model = @klass.new
instance = @model.instance
raw_model = @model.model

args = params[@klass.name.underscore]
transforms = args.delete(:transforms) || {}
args = params[@klass.underscore_name]

args.each do |key, value|
# TODO(jon): Figure out a better way to do transforms.
# This could cause issues and is exploitable, but it
# should be fine for now since this is only on admin pages
if transforms[key] and not value.blank?
value = transform(transforms, key, value)
end
# TODO(jon): Figure out a way to do transforms.

# TODO(jon): Remove duplicate code between update and create
if args["#{key}_is_nil"] == "1"
instance.send("#{key}=", nil)
raw_model.send("#{key}=", nil)
else
if key.ends_with?("_is_nil")
# Skip this, since we use the non _is_nil arg.
else
instance.send("#{key}=", value)
raw_model.send("#{key}=", value)
end
end
end

if instance.save
flash[:notice] = "#{@klass.humanized_name(:singular)} created successfully with id=#{instance.id}."
redirect_to(upmin_model_path(@model.path_hash))
if raw_model.save
flash[:notice] = "#{@klass.humanized_name(:singular)} created successfully with id=#{raw_model.id}."
redirect_to(@model.path)
else
flash.now[:alert] = "#{@klass.humanized_name(:singular)} was NOT created."
render(:new)
Expand All @@ -62,72 +57,70 @@ def create

# PUT /:model_name/:id
def update
instance = @model.instance
updates = params[@klass.name.underscore]
transforms = updates.delete(:transforms) || {}

updates.each do |key, value|
# TODO(jon): Figure out a better way to do transforms.
# This could cause issues and is exploitable, but it
# should be fine for now since this is only on admin pages
if transforms[key] and not value.blank?
value = transform(transforms, key, value)
end
raw_model = @model.model
updates = params[@klass.underscore_name]

updates.each do |key, value|
# TODO(jon): Remove duplicate code between update and create
if updates["#{key}_is_nil"] == "1"
instance.send("#{key}=", nil)
raw_model.send("#{key}=", nil)
else
if key.ends_with?("_is_nil")
# Skip this, since we use the non _is_nil arg.
else
instance.send("#{key}=", value)
raw_model.send("#{key}=", value)
end
end
end

if instance.save
if raw_model.save
flash[:notice] = "#{@klass.humanized_name(:singular)} updated successfully."
redirect_to(upmin_model_path(@model.path_hash))
redirect_to(@model.path)
else
flash.now[:alert] = "#{@klass.humanized_name(:singular)} was NOT updated."
render(:show)
end
end

def search
@q = @klass.ransack(params[:q])
@results = Upmin::Paginator.paginate(@q.result(distinct: true), @page, 30)
# @q = @klass.ransack(params[:q])
# @results = Upmin::Paginator.paginate(@q.result(distinct: true), @page, 30)
end

def action
# begin
response = @model.perform_action(params[:method], @arguments)
flash[:notice] = "Action successfully performed with a response of: #{response}"
redirect_to(upmin_model_path(@model.path_hash))
@response = @action.perform(@arguments)

flash[:notice] = "Action successfully performed with a response of: #{@response}"
redirect_to(@model.path)
# rescue Exception => e
# flash.now[:alert] = "Action failed with the error message: #{e.message}"
# render(:show)
# end
end

private

def set_klass
@klass = Upmin::Klass.find(params[:klass])
raise "Invalid klass name" if @klass.nil?
def set_query
@query = Upmin::Query.new(@klass, params[:q], page: @page, per_page: 30)
end

def set_model
@model = @klass.find(params[:id])
@model = @klass.new(id: params[:id])
end

def set_method
@method = params[:method].to_sym
def set_klass
@klass = Upmin::Model.find_class(params[:klass])
end

def set_action
action_name = params[:method].to_sym
@action = @model.actions.select{ |action| action.name == action_name }.first

raise Upmin::InvalidAction.new(params[:method]) unless @action
end

def set_arguments
arguments = params[@method] || {}
arguments = params[@action.name] || {}
@arguments = {}
arguments.each do |k, v|
unless k.ends_with?("_is_nil")
Expand All @@ -136,6 +129,7 @@ def set_arguments
end
end
end
@arguments = ActiveSupport::HashWithIndifferentAccess.new(@arguments)
end

def set_page
Expand Down
13 changes: 0 additions & 13 deletions app/helpers/upmin/instances_helper.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/views/layouts/upmin/_navbar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
%span.icon-bar
#navbar-main.navbar-collapse.collapse
%ul.nav.navbar-nav
- Upmin::Klass.all.each do |m|
- Upmin::Model.all.each do |m|
%li
%a{href: upmin_search_path(klass: m.name)}
= m.name.pluralize
%a{href: m.search_path}
= m.humanized_name(:plural)
6 changes: 3 additions & 3 deletions app/views/upmin/models/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
%button.close{"data-dismiss" => "alert", type: "button"}
%span{"aria-hidden" => "true"} &times;
= alert
- if @model.instance.errors.any?
- if @model.errors.any?
%ul
- @model.instance.errors.each do |field, error|
- @model.errors.each do |field, error|
%li
%b
= field
= error
.row
.col-sm-12
= up_model(@model.instance)
= up_render(@model)
8 changes: 4 additions & 4 deletions app/views/upmin/models/search.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
.row
.col-md-8
-# TODO(jon): Add pagination w/ search results
= up_search_results(@q, @results)
= up_render(@query)
%br
= up_paginate(@results)
= up_paginate(@query.paginated_results)
%br
%br
.col-md-4
-# TODO(jon): Implement up_search_box
= up_search_box(@klass)
= up_render(@klass)
.new-button-wrapper
%a.btn.btn-block.btn-success{href: upmin_new_model_path(klass: @klass.name)}
%a.btn.btn-block.btn-success{href: upmin_new_model_path(klass: @klass.model_class_name)}
Create a new
= @klass.humanized_name(:singular)
%br
6 changes: 3 additions & 3 deletions app/views/upmin/models/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
%button.close{"data-dismiss" => "alert", type: "button"}
%span{"aria-hidden" => "true"} &times;
= alert
- if @model.instance.errors.any?
- if @model.errors.any?
%ul
- @model.instance.errors.each do |field, error|
- @model.errors.each do |field, error|
%li
%b
= field
= error
.row
.col-sm-12
= up_model(@model.instance)
= up_render(@model)
27 changes: 6 additions & 21 deletions app/views/upmin/partials/actions/_action.html.haml
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
%h4{style: "color: #333;"}
= action.title
.well.action-well
= form_tag(upmin_action_path(upmin_model.path_hash.merge(method: action_name))) do
- upmin_model.action_parameters(action_name).each do |param_type, param_name|

- next if param_type == :block # Skip blocks

.form-group
= label(action_name, param_name, param_name.to_s.capitalize.gsub("_", " "))

- if param_type == :opt
.input-group
= text_field(action_name, param_name, class: "form-control")
.input-group-addon.nilable-addon
.form-group
%label{for: "#{action_name}_#{param_name}_is_nil"}
Do Not Provide
= check_box(action_name, "#{param_name}_is_nil", class: "boolean")
%small
* Optional
- else
= text_field(action_name, param_name, class: "form-control")

= form_tag(action.path, class: action.name) do
- action.parameters.each do |parameter|
-# = Upmin::Railties::RenderHelpers.parameter_partials(parameter)
= up_render(parameter)

= submit_tag("Submit", class: "btn btn-primary")
18 changes: 8 additions & 10 deletions app/views/upmin/partials/associations/_associations.html.haml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
- if associations.any?
%p
- associations.each do |u|
- nested_upmin_model = Upmin::Model.new(u)
%a.active-tag-link{href: upmin_model_path(nested_upmin_model.path_hash)}
%span.label{class: nested_upmin_model.color}
= nested_upmin_model.title
%h5
= association.title

- else
- if association.empty?
%p.well
None



- else
- association.upmin_values.each do |m|
%a.active-tag-link{href: m.path}
%span.label{class: m.color}
= m.title
Loading