Skip to content

Commit

Permalink
adding model finder to query models
Browse files Browse the repository at this point in the history
  • Loading branch information
subbarao committed Mar 3, 2013
1 parent f9c83c1 commit 2f387f6
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 112 deletions.
8 changes: 7 additions & 1 deletion lib/admin_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module AdminData

LIBPATH = File.dirname(__FILE__)


class NoCreatedAtColumnException < Exception
end



autoload :Configuration
autoload :Util
autoload :Config
Expand All @@ -15,10 +21,10 @@ module AdminData
autoload :Authenticator
autoload :Search
autoload :Analytics
autoload :ModelFinder

include SetupConfig

end

require 'admin_data/railtie'
require 'admin_data/exceptions'
5 changes: 0 additions & 5 deletions lib/admin_data/exceptions.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/admin_data/model_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module AdminData
module ModelFinder
extend self

def models
ActiveRecord::Base.send(:subclasses).map do | klass |
namespaced_models(klass).push(klass.name)
end.flatten.sort
end

def namespaced_models(model_klass)
model_klass.subclasses.map(&:name)
end
end
end
3 changes: 0 additions & 3 deletions test/dummy/app/models/car.rb

This file was deleted.

22 changes: 0 additions & 22 deletions test/dummy/app/models/city.rb

This file was deleted.

3 changes: 0 additions & 3 deletions test/dummy/app/models/club.rb

This file was deleted.

4 changes: 0 additions & 4 deletions test/dummy/app/models/newspaper.rb

This file was deleted.

3 changes: 0 additions & 3 deletions test/dummy/app/models/phone_number.rb

This file was deleted.

14 changes: 0 additions & 14 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
class User < ActiveRecord::Base
default_scope :order => 'id'

serialize :data

has_many :phone_numbers, :dependent => :destroy
has_and_belongs_to_many :clubs
has_one :website, :dependent => :destroy

validates_presence_of :first_name, :age
validates_numericality_of :age

def to_param
"#{self.id}-#{self.first_name}-#{self.last_name}".downcase.gsub(/\W/, ' ').strip.gsub(' ', '-').squeeze('-')
end

end
2 changes: 2 additions & 0 deletions test/dummy/app/models/user/student.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User::Student < User
end
3 changes: 0 additions & 3 deletions test/dummy/app/models/website.rb

This file was deleted.

Binary file modified test/dummy/db/development.sqlite3
Binary file not shown.
17 changes: 14 additions & 3 deletions test/dummy/db/migrate/20091030202259_create_tables.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateTables < ActiveRecord::Migration
=begin
def self.up

create_table :cars do |t|
t.string :name
t.timestamps
Expand All @@ -9,10 +9,11 @@ def self.up
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :type
t.integer :age
t.text :data
t.text :data
t.boolean :active, :default => false
t.text :description
t.text :description
t.datetime :born_at
t.timestamps
end
Expand Down Expand Up @@ -58,4 +59,14 @@ def self.up
def self.down
drop_table :users
end
=end

def change
create_table :users do |t|
t.string :type
t.string :name

t.timestamps
end
end
end
55 changes: 4 additions & 51 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,11 @@

ActiveRecord::Schema.define(:version => 20091030202259) do

create_table "cars", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "cities", :force => true do |t|
t.string "name"
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "clubs", :force => true do |t|
t.string "name"
end

create_table "clubs_users", :id => false, :force => true do |t|
t.integer "user_id", :null => false
t.integer "club_id", :null => false
end

create_table "papers", :id => false, :force => true do |t|
t.string "paper_id", :null => false
t.string "name"
end

create_table "phone_numbers", :force => true do |t|
t.string "number"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.integer "age"
t.text "data"
t.boolean "active", :default => false
t.text "description"
t.datetime "born_at"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "websites", :force => true do |t|
t.string "url"
t.integer "user_id"
t.string "dns_provider", :default => "yahoo"
t.datetime "created_at"
t.datetime "updated_at"
t.string "type"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
Binary file modified test/dummy/db/test.sqlite3
Binary file not shown.
16 changes: 16 additions & 0 deletions test/unit/admin_data/model_finder_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "test_helper"

class AdminData::ModelFinderTest < ActiveSupport::TestCase

test "list all model from app" do
expected = %w(User User::Student).sort

assert_equal expected, AdminData::ModelFinder.models
end

test "namespaced models" do
expected = %w(User::Student)
assert_equal expected, AdminData::ModelFinder.namespaced_models(User)
end

end

0 comments on commit 2f387f6

Please sign in to comment.