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

Rails 4 cleanup #6

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
439f5c9
NBD, a couple years of updates from upstream repos.
quirkey Mar 1, 2014
a0b76ac
Have to add a seperate railtie file, config considered harmful
quirkey Mar 2, 2014
e6f8db5
Rails 4 Changes
quirkey Mar 2, 2014
c85fff7
Use Erubis instead of erb (erubis is default in rails 3+)
quirkey Mar 15, 2014
d2c8024
Add model_params that checks for strong_parameters
quirkey Mar 16, 2014
37143e9
resolve conflicts
arirusso Feb 26, 2015
e3f03ae
Merge branch 'dev_improvements' of github.com:arirusso/qadmin into ra…
arirusso Feb 26, 2015
63d0227
Merge branch 'dev_improvements' of github.com:arirusso/qadmin into ra…
arirusso Feb 26, 2015
be1139f
Merge branch 'master' of https://github.com/quirkey/qadmin into rails…
arirusso Feb 26, 2015
4d5fd05
use minitest
arirusso Feb 26, 2015
fe67977
add appraisal file for rails 2-4
arirusso Feb 26, 2015
74c084e
add erubis
arirusso Feb 26, 2015
97aeb52
load new dependencies
arirusso Feb 26, 2015
b32b1e2
use forwardable
arirusso Feb 26, 2015
b350ae0
naming
arirusso Feb 26, 2015
6a62721
Merge branch 'rails4_tests' of github.com:arirusso/qadmin into rails4…
arirusso Feb 26, 2015
d86c545
naming
arirusso Feb 26, 2015
6c1dd78
Merge branch 'rails4_tests' of github.com:arirusso/qadmin into rails4…
arirusso Feb 26, 2015
64a782f
ease off the module eval and such
arirusso Feb 26, 2015
08ed412
readability
arirusso Feb 26, 2015
0b1972d
var name change
arirusso Feb 26, 2015
f70a56a
initialize properly
arirusso Feb 26, 2015
edf01b4
models must now exist
arirusso Feb 26, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
appraise "rails-2" do
gem "iconv"
gem "rails", "2.3.18"
end

appraise "rails-3" do
gem "rails", "3.2.14"
end

appraise "rails-4" do
gem "rails", "4.1.8"
end
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ begin
s.rubyforge_project = %q{quirkey}
s.summary = %q{An [almost] one command solution for adding admin interfaces/resources to a Rails app.}
s.add_runtime_dependency(%q<activesupport>, [">= 2.3.2", "< 3"])
s.add_runtime_dependency(%q<erubis>, ["~> 2.7", ">= 2.7.0"])
s.add_runtime_dependency(%q<iconv>, [">= 1.0"])
s.add_runtime_dependency(%q<restful_query>, [">= 0.2.0"])
s.add_runtime_dependency(%q<will_paginate>, [">= 2.3.7", "< 3"])
s.add_development_dependency(%q<appraisal>, ["~>1.0", ">= 1.0.2"])
s.add_development_dependency(%q<minitest>, ["~> 5.5", ">= 5.5.0"])
s.add_development_dependency(%q<mocha>, ["~> 1.1", ">= 1.1.0"])
s.add_development_dependency(%q<rake>, ["~>10.4", ">= 10.4.2"])
s.add_development_dependency(%q<rails>, [">= 2.3.2", "< 3"])
Expand All @@ -32,7 +35,7 @@ end
Rake::TestTask.new do |t|
should_test_generator = ENV["TEST_GENERATOR"] == "true"
t.libs << "test"
files = FileList["test/**/*_test.rb"].delete_if { |f| !should_test_generator && f == "test/qadmin_generator_test.rb" }
files = FileList["test/**/*_test.rb"].delete_if { |f| !should_test_generator && f == "test/generator_test.rb" }
t.test_files = files
t.verbose = true
end
Expand Down
8 changes: 3 additions & 5 deletions lib/qadmin.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'iconv'

unless defined?(ActiveSupport)
require 'active_support'
end

require 'erb'
require "erubis"
require 'forwardable'

module Qadmin
VERSION = '0.2.3'
VERSION = '0.3.0'
end

%w{
Expand Down
6 changes: 4 additions & 2 deletions lib/qadmin/actions/create.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
def create
logger.debug 'Qadmin: Default /create'
@model_instance = @<%= config.on_create.model_instance_name %> = <%= config.on_create.model_name %>.new(params[:<%= config.on_create.model_instance_name %>])
@model_instance = @<%= config.on_create.model_instance_name %> = <%= config.on_create.model_name %>.new(<%= config.on_create.model_instance_name %>_params)
respond_to do |format|
if @<%= config.on_create.model_instance_name %>.save
flash[:message] = '<%= config.on_create.model_human_name %> was successfully created.'
format.html { redirect_to(<%= config.on_create.path_prefix %>_path(@<%= config.on_create.model_instance_name %>)) }
format.xml { render :xml => @<%= config.on_create.model_instance_name %>, :status => :created, :location => @<%= config.on_create.model_instance_name %> }
format.js { render :json => {:<%= config.on_create.model_instance_name %> => @<%= config.on_create.model_instance_name %>, :status => :success}}
else
format.html { render_template_for_section('new.html') }
format.xml { render :xml => @<%= config.on_create.model_instance_name %>.errors }
format.js { render :json => {:status => :error, :messages => @<%= config.on_create.model_instance_name %>.errors}}
end
end
end
end
2 changes: 1 addition & 1 deletion lib/qadmin/actions/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def index
format.xml
format.js
end
end
end
2 changes: 1 addition & 1 deletion lib/qadmin/actions/update.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def update
@model_instance = @<%= config.on_update.model_instance_name %> = <%= config.on_update.model_name %>.find(params[:id])

respond_to do |format|
if @<%= config.on_update.model_instance_name %>.update_attributes(params[:<%= config.on_update.model_instance_name %>])
if @<%= config.on_update.model_instance_name %>.update_attributes(<%= config.on_update.model_instance_name %>_params)
flash[:message] = '<%= config.on_update.model_human_name %> was successfully updated.'
format.html { redirect_to(<%= config.on_update.path_prefix %>_path(@<%= config.on_update.model_instance_name %>)) }
format.xml { head :ok }
Expand Down
238 changes: 138 additions & 100 deletions lib/qadmin/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,133 +1,159 @@
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/string/inflections'

module Qadmin
module Configuration

class Base < ::HashWithIndifferentAccess

attr_accessor :base


attr_reader :base,
:controller_class,
:controller_name,
:model_class,
:model_name,
:model_instance_name,
:model_collection_name,
:model_human_name,
:namespace,
:parent,
:default_scope

def with_indifferent_access
self
end

def self.hash_accessor(name, options = {})
options[:default] ||= nil
coerce = options[:coerce] ? ".#{options[:coerce]}" : ""
module_eval <<-EOT
def #{name}
value = (self[:#{name}] ? self[:#{name}]#{coerce} : self[:#{name}]) ||
(base && base.respond_to?(:#{name}) ? base.send(:#{name}) : #{options[:default].inspect})
yield value if block_given?
value
end

def #{name}=(value)
self[:#{name}] = value
end

def #{name}?
!!self[:#{name}]
end
EOT
end

hash_accessor :controller_klass
hash_accessor :controller_name
hash_accessor :model_name
hash_accessor :model_instance_name
hash_accessor :model_collection_name
hash_accessor :model_human_name
hash_accessor :namespace, :default => false
hash_accessor :parent, :default => false
hash_accessor :default_scope, :default => false

def initialize(options = {})
super
@base = options.delete(:base)
extract_model_from_options(options)
@namespace = false
@parent = false
@default_scope = false
populate_model(options)
end

def model_klass
self.model_name.constantize
end

def path_prefix(plural = false)
name = plural ? model_collection_name : model_instance_name
if namespace
"#{namespace}_#{name}"
else
name = plural ? @model_collection_name : @model_instance_name
if @namespace.nil?
name
else
"#{@namespace}_#{name}"
end
end

def polymorphic_array(*args)
args.compact!
if @parent && args.length < 2
args.unshift(@parent)
end
args.unshift(@namespace) if @namespace
args
end

def form_instance_for(instance)
if parent
[parent, instance]
elsif namespace
[namespace, instance]
else
instance
end
i = instance.class != @model_class ? instance.becomes(@model_class) : instance
polymorphic_array(i)
end

def model_column_names
model_klass.column_names
@columns ||= @model_class.column_names
rescue
[]
end

def inspect
"#<#{self.class} #{super}>"
end

protected
def extract_model_from_options(options = {})
self.controller_klass = options[:controller_klass]
self.controller_name = options[:controller_name] || controller_klass.to_s.demodulize.gsub(/Controller/,'').underscore
self.model_name = options[:model_name] || controller_klass.to_s.demodulize.gsub(/Controller/,'').singularize
self.model_instance_name = options[:model_instance_name] || model_name.underscore
self.model_collection_name = options[:model_collection_name] || model_instance_name.pluralize
self.model_human_name = options[:model_human_name] || model_instance_name.humanize

possible_namespace = controller_klass.to_s.underscore.split('/')[0]
self.namespace = options[:namespace] || (possible_namespace =~ /controller/) ? nil : possible_namespace.to_sym

private

# Turn a set of options into the full options needed for configuration
def populate_model(properties)
@controller_class = properties[:controller_class]
@controller_name = properties[:controller_name] || @controller_class.to_s.demodulize.gsub(/Controller/,'').underscore
@model_name = properties[:model_name] || @controller_class.to_s.demodulize.gsub(/Controller/,'').singularize
@model_instance_name = properties[:model_instance_name] || @model_name.underscore
@model_collection_name = properties[:model_collection_name] || @model_instance_name.pluralize
@model_human_name = properties[:model_human_name] || @model_instance_name.humanize
populate_namespace(properties[:namespace])
@model_class = @model_name.constantize
self
end

def populate_namespace(namespace)
if match = @controller_class.to_s.match(/\A(.+)::\w+\z/)
modules = match[1]
@namespace = modules.underscore unless modules.nil?
end
@namespace ||= namespace
@namespace = @namespace.to_sym unless @namespace.nil?
end

end

module Actions
class Action < Qadmin::Configuration::Base
hash_accessor :multipart_forms, :default => false
hash_accessor :controls, :default => []

attr_reader :multipart_forms,
:controls,
:control_links

def initialize(options = {})
super
@multipart_forms = false
@controls = []
@control_links = {}
end

end

class Index < Action
hash_accessor :columns, :default => []
hash_accessor :column_headers, :default => {}
hash_accessor :column_css, :default => {}
hash_accessor :row_controls, :default => [:show, :edit, :destroy]
hash_accessor :attribute_handlers, :default => {}

attr_reader :columns,
:column_headers,
:column_css,
:controls,
:row_controls,
:attribute_handlers

def initialize(options = {})
super
self.columns = model_column_names
@columns = model_column_names
@column_headers = {}
@column_css = {}
@controls = [:new]
@row_controls = [:show, :edit, :destroy]
@attribute_handlers = {}
end

end

class Show < Action

def initialize(options = {})
super
@controls = [:index, :new, :edit, :destroy]
end

end

class New < Action

def initialize(options = {})
super
@controls = [:index]
end

end

class Edit < Action

def initialize(options = {})
super
@controls = [:index, :new, :show, :destroy]
end

end

class Create < Action

end

class Update < Action
Expand All @@ -137,32 +163,44 @@ class Update < Action
class Destroy < Action

end

end

class Resource < Base

ACTIONS = [:index, :show, :new, :create, :edit, :update, :destroy].freeze

hash_accessor :available_actions, :default => ACTIONS.dup
hash_accessor :ports, :default => false

hash_accessor :multipart_forms, :default => false
hash_accessor :controls, :default => []

ACTIONS.each do |action|
hash_accessor "on_#{action}"

module_eval <<-EOV
def on_#{action}
value = self["on_#{action}"] ||= "Qadmin::Configuration::Actions::#{action.to_s.classify}".constantize.new(self.dup.merge(:base => self))
yield value if block_given?
value

attr_accessor :available_actions
attr_reader :ports,
:multipart_forms,
:control_links

def initialize(options = {})
super
@available_actions = options[:available_actions] || ACTIONS.dup
@ports = false
@multipart_forms = false
@control_links = {}
@actions = {}
populate_model(options)
end

def method_missing(method, *args, &block)
if match = method.to_s.match(/\Aon\_(\w+)/)
action = match[1]
if @available_actions.include?(action.to_sym) && (value = @actions[action]).nil?
klass = "Qadmin::Configuration::Actions::#{action.to_s.classify}".constantize
value = klass.new(:controller_class => @controller_class)
@actions[action] = value
end
EOV
yield(value) if block_given?
value
else
super
end
end

end

end
end
end
Loading