Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion lib/static_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def marked_for_destruction?
def valid?
true
end

# For compatibility with AR relations in ActiveAdmin and others.
# Feel free to override this.
def self.where(*args)
Expand Down Expand Up @@ -112,6 +112,13 @@ def static_models_hashes(columns, hashes)
raise ValueError.new if singleton_methods.include?(item.code)
define_singleton_method(item.code){ item }
end

def arel_table
@arel_table ||= begin
table_name = self.to_s.split('::').map(&:underscore).map(&:pluralize).join('_')
Arel::Table.new(table_name, type_caster: ::StaticModels::FakeTypeCaster.new)
end
end
end

def find(id)
Expand Down Expand Up @@ -202,6 +209,12 @@ def belongs_to(association, opts = {})
end
end

class FakeTypeCaster
def type_cast_for_database(_attr_name, value)
value
end
end

class ValueError < StandardError; end
class NotFoundError < StandardError; end
end
2 changes: 1 addition & 1 deletion lib/static_models/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module StaticModels
VERSION = "0.4.8"
VERSION = "0.5.0"
end
4 changes: 4 additions & 0 deletions spec/static_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ class ValidDog < ActiveRecord::Base
dog.update(anything: dog)
dog.should be_valid
end

it 'can be used in queries' do
expect(StoreDog.where(breed: Breed.collie).count).to be_zero
end
end

it "has a code accessor useful for validations" do
Expand Down