Permalink
Browse files

Added support for mapping model attributes to interface attributes wi…

…th a different name
  • Loading branch information...
1 parent 53fa6f0 commit 8fa111095e6bacc2cd50c533c90f83d0160d8707 @vihai vihai committed Jun 8, 2012
@@ -160,14 +160,21 @@ def autoinitialize_attrs_from_ar_model
def attribute(name, type = nil, &block)
a = @attrs || @delayed_attrs
+ name_in_model = name
+ if name.is_a?(Hash)
+ name_in_model, name = name.keys.first, name.values.first
+ end
+
if type
begin
a[name] ||= "ActiveRest::Model::Interface::Attribute::#{type}".constantize.new(name, self)
rescue NameError
- a[name] ||= Attribute.new(name, self, :type => type.split('::').last.underscore.to_sym)
+ a[name] ||= Attribute.new(name, self,
+ :type => type.split('::').last.underscore.to_sym,
+ :name_in_model => name_in_model)
end
else
- a[name] ||= Attribute.new(name, self)
+ a[name] ||= Attribute.new(name, self, :name_in_model => name_in_model)
end
a[name].instance_exec(&block) if block
@@ -263,12 +270,12 @@ def eager_loading_hints(opts = {})
if !viewdef || !viewdef.limit
subinc = attr.model_class.constantize.interfaces[@name].eager_loading_hints(:view => subview)
- incs << (subinc.any? ? { attrname => subinc } : attrname)
+ incs << (subinc.any? ? { attr.name_in_model => subinc } : attr.name_in_model)
end
when Model::Interface::Attribute::UniformReferencesCollection
if viewinc && !viewdef.limit
subinc = attr.referenced_class_name.constantize.interfaces[@name].eager_loading_hints(:view => subview)
- incs << (subinc.any? ? { attrname => subinc } : attrname)
+ incs << (subinc.any? ? { attr.name_in_model => subinc } : attr.name_in_model)
end
when Model::Interface::Attribute::EmbeddedPolymorphicModel
when Model::Interface::Attribute::PolymorphicReference
@@ -320,47 +327,47 @@ def ar_serializable_hash(obj, opts = {})
case attr
when Model::Interface::Attribute::Structure
- val = obj.send(attrname)
+ val = obj.send(attr.name_in_model)
values[attrname] = val ? val.ar_serializable_hash(@name, opts.merge(:view => subview)) : nil
when Model::Interface::Attribute::Reference
if viewinc
- val = obj.send(attrname)
+ val = obj.send(attr.name_in_model)
values[attrname] = val ? val.ar_serializable_hash(@name, opts.merge(:view => subview)) : nil
end
when Model::Interface::Attribute::EmbeddedModel
- val = obj.send(attrname)
+ val = obj.send(attr.name_in_model)
values[attrname] = val ? val.ar_serializable_hash(@name, opts.merge(:view => subview)) : nil
when Model::Interface::Attribute::UniformModelsCollection
- vals = obj.send(attrname)
+ vals = obj.send(attr.name_in_model)
if viewdef
vals = vals.limit(viewdef.limit) if viewdef.limit
vals = vals.order(viewdef.order) if viewdef.order
end
values[attrname] = vals.map { |x| x.ar_serializable_hash(@name, opts.merge(:view => subview)) }
when Model::Interface::Attribute::UniformReferencesCollection
if viewinc
- vals = obj.send(attrname)
+ vals = obj.send(attr.name_in_model)
if viewdef
vals = vals.limit(viewdef.limit) if viewdef.limit
vals = vals.order(viewdef.order) if viewdef.order
end
values[attrname] = vals.map { |x| x.ar_serializable_hash(@name, opts.merge(:view => subview)) }
end
when Model::Interface::Attribute::EmbeddedPolymorphicModel
- val = obj.send(attrname)
+ val = obj.send(attr.name_in_model)
values[attrname] = val ? val.ar_serializable_hash(@name, opts.merge(:view => subview)) : nil
when Model::Interface::Attribute::PolymorphicReference
if viewinc
- val = obj.send(attrname)
+ val = obj.send(attr.name_in_model)
values[attrname] = val ? val.ar_serializable_hash(@name, opts.merge(:view => subview)) : nil
else
- ref = obj.association(attrname).reflection
+ ref = obj.association(attr.name_in_model).reflection
values[attrname] = { :id => obj.send(ref.foreign_key), :_type => obj.send(ref.foreign_type) }
end
when Model::Interface::Attribute::PolymorphicModelsCollection
when Model::Interface::Attribute::PolymorphicReferencesCollection
else
- val = obj.send(attrname)
+ val = obj.send(attr.name_in_model)
if !val.nil?
case attr.type
@@ -538,7 +545,7 @@ def map_column_type(type)
end
def to_s
- "<#{self.class.name} model=#{@model.class.name} name=#{@name} ai=#{@activerecord_autoinit}"
+ "<#{self.class.name} model=#{@model.class.name} name=#{@name} ai=#{@activerecord_autoinit}>"
end
class AssociatedRecordNotFound < StandardError
@@ -9,6 +9,7 @@ class Interface
class Attribute
attr_accessor :name
+ attr_accessor :name_in_model
attr_accessor :type
attr_accessor :interface
attr_accessor :human_name
@@ -20,9 +21,15 @@ class Attribute
attr_accessor :writable
def initialize(name, interface, h = {})
+ raise ArgumentError, 'Name can not be null' if !name
+
@name = name
+ @name_in_model = name
+
@interface = interface
+ @type = :string
+
@human_name = ''
@meta = {}
@default = nil
@@ -66,6 +73,7 @@ def definition
def apply(attr)
# type ?
+ @name_in_model = attr.name_in_model if attr.name_in_model
@human_name = attr.human_name
@meta.merge!(attr.meta)
@ignored = attr.ignored
@@ -92,8 +100,8 @@ def not_readable!
end
def to_s
- "<#{self.class.name} name=#{@name} human_name=#{@human_name} default=#{@default} not_null=#{@not_null}" +
- "flags=#{@ignored ? 'I' : ''}#{@readable ? 'R' : ''}#{@writable ? 'W' : ''}"
+ "<#{@type.capitalize} #{@name_in_model} => #{@name} human_name=#{@human_name} default=#{@default} not_null=#{@not_null}" +
+ "flags=#{@ignored ? 'I' : ''}#{@readable ? 'R' : ''}#{@writable ? 'W' : ''}>"
end
#
@@ -1,3 +1,3 @@
module ActiveRest
- VERSION = '4.8.1'
+ VERSION = '4.9.0'
end
View
@@ -14,58 +14,58 @@ GIT
PATH
remote: ..
specs:
- active_rest (4.7.1)
+ active_rest (4.9.0)
GEM
remote: http://rubygems.org/
specs:
- actionmailer (3.2.3)
- actionpack (= 3.2.3)
+ actionmailer (3.2.5)
+ actionpack (= 3.2.5)
mail (~> 2.4.4)
- actionpack (3.2.3)
- activemodel (= 3.2.3)
- activesupport (= 3.2.3)
+ actionpack (3.2.5)
+ activemodel (= 3.2.5)
+ activesupport (= 3.2.5)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.1)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
- sprockets (~> 2.1.2)
- activemodel (3.2.3)
- activesupport (= 3.2.3)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.5)
+ activesupport (= 3.2.5)
builder (~> 3.0.0)
- activerecord (3.2.3)
- activemodel (= 3.2.3)
- activesupport (= 3.2.3)
+ activerecord (3.2.5)
+ activemodel (= 3.2.5)
+ activesupport (= 3.2.5)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
- activeresource (3.2.3)
- activemodel (= 3.2.3)
- activesupport (= 3.2.3)
- activesupport (3.2.3)
+ activeresource (3.2.5)
+ activemodel (= 3.2.5)
+ activesupport (= 3.2.5)
+ activesupport (3.2.5)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
diff-lcs (1.1.3)
erubis (2.7.0)
- factory_girl (3.2.0)
+ factory_girl (3.3.0)
activesupport (>= 3.0.0)
- factory_girl_rails (3.2.0)
- factory_girl (~> 3.2.0)
+ factory_girl_rails (3.3.0)
+ factory_girl (~> 3.3.0)
railties (>= 3.0.0)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.3)
- json (1.7.1)
+ json (1.7.3)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.18)
- multi_json (1.3.4)
- nokogiri (1.5.2)
+ multi_json (1.3.6)
+ nokogiri (1.5.3)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
@@ -74,29 +74,29 @@ GEM
rack
rack-test (0.6.1)
rack (>= 1.0)
- rails (3.2.3)
- actionmailer (= 3.2.3)
- actionpack (= 3.2.3)
- activerecord (= 3.2.3)
- activeresource (= 3.2.3)
- activesupport (= 3.2.3)
+ rails (3.2.5)
+ actionmailer (= 3.2.5)
+ actionpack (= 3.2.5)
+ activerecord (= 3.2.5)
+ activeresource (= 3.2.5)
+ activesupport (= 3.2.5)
bundler (~> 1.0)
- railties (= 3.2.3)
- railties (3.2.3)
- actionpack (= 3.2.3)
- activesupport (= 3.2.3)
+ railties (= 3.2.5)
+ railties (3.2.5)
+ actionpack (= 3.2.5)
+ activesupport (= 3.2.5)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
- thor (~> 0.14.6)
+ thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
- rspec-core (2.10.0)
+ rspec-core (2.10.1)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
@@ -112,7 +112,7 @@ GEM
sqlite3 (1.3.6)
sqlite3-ruby (1.3.3)
sqlite3 (>= 1.3.3)
- thor (0.14.6)
+ thor (0.15.2)
tilt (1.3.3)
treetop (1.4.10)
polyglot
@@ -3,22 +3,22 @@ class Company < ActiveRecord::Base
class FullAddress
include ActiveRest::Model
- attr_reader :address, :city, :zip
+ attr_reader :street, :city, :zip
interface :rest do
- attribute(:address)
+ attribute(:street)
attribute(:city)
attribute(:zip)
end
- def initialize(address, city, zip)
- @address = address
+ def initialize(street, city, zip)
+ @street = street
@city = city
@zip = zip
end
def to_s
- [ @address, @city, @zip ].compact.join(', ')
+ [ @street, @city, @zip ].compact.join(', ')
end
end
@@ -64,9 +64,9 @@ class Bar < ActiveRecord::Base
composed_of :full_address,
:class_name => '::Company::FullAddress',
:mapping => [
- [ :address, :address ],
+ [ :street, :street ],
[ :city, :city ],
- [ :city, :zip ],
+ [ :zip, :zip ],
]
belongs_to :object_1, :polymorphic => true, :embedded => true
@@ -23,9 +23,11 @@
'created_at' => { 'type' => 'timestamp' },
'updated_at' => { 'type' => 'timestamp' },
'name' => { 'type' => 'string', 'human_name' => 'Nome' },
- 'city' => { 'type' => 'string' },
- 'street' => {'type' => 'string' },
- 'zip' => {'type' => 'string' },
+ 'full_address' => {
+ 'city' => { 'type' => 'string' },
+ 'street' => {'type' => 'string' },
+ 'zip' => {'type' => 'string' },
+ },
'is_active' => {'type' => 'boolean' },
'registration_date' => {'type' => 'timestamp' },
'group_id' => { 'type' => 'integer' },
@@ -127,11 +129,13 @@
b = ActiveSupport::JSON.decode(response.body)
b.should deep_include({
- 'city' => 'Springfield',
'id' => 2,
'name' => 'compuglobal',
- 'street' => 'Bart\'s road',
- 'zip' => '513',
+ 'full_address' => {
+ 'city' => 'Springfield',
+ 'street' => 'Bart\'s road',
+ 'zip' => '513',
+ },
})
end
@@ -29,11 +29,13 @@
response.should be_success
ActiveSupport::JSON.decode(response.body).should deep_include({
- 'city' => 'Springfield',
'id' => 2,
'name' => 'compuglobal',
- 'street' => 'Bart\'s road',
- 'zip' => '513',
+ 'full_address' => {
+ 'city' => 'Springfield',
+ 'street' => 'Bart\'s road',
+ 'zip' => '513',
+ },
})
end
Oops, something went wrong.

0 comments on commit 8fa1110

Please sign in to comment.