diff --git a/lib/deja/schema_generator.rb b/lib/deja/schema_generator.rb index 9f43873..8c8b41d 100644 --- a/lib/deja/schema_generator.rb +++ b/lib/deja/schema_generator.rb @@ -7,16 +7,19 @@ module ClassMethods @@all_attributes ||= {} @@indexed_attributes ||= {} @@composed_attributes ||= {} + @@editable_attributes ||= {} def schema { :attributes => inspect_attributes, + :editable_attributes => inspect_editable_attributes, :validations => inspect_validations } end def define_class_key @@all_attributes[self.name] ||= {} + @@editable_attributes[self.name] ||= [] end def attribute(name, type, opts = {}) @@ -25,6 +28,7 @@ def attribute(name, type, opts = {}) @@all_attributes[self.name][sym_name] = opts.merge(:type => type) attr_accessorize(sym_name, opts) add_property_to_index(sym_name) if opts[:index] + @@editable_attributes[self.name] << sym_name if opts[:editable] != false end def attr_accessorize(name, opts) @@ -36,6 +40,10 @@ def attr_accessorize(name, opts) end end + def editable_attributes + @@editable_attributes + end + def indexed_attributes @@indexed_attributes end @@ -68,7 +76,6 @@ def composed_attributes(attrs = nil) def inspect_attributes klass = self attrs = {} - while @@all_attributes.has_key?(klass.name) attrs.merge!(@@all_attributes[klass.name]) klass = klass.superclass @@ -77,6 +84,16 @@ def inspect_attributes attrs end + def inspect_editable_attributes + klass = self + attrs = [] + while @@editable_attributes.has_key?(klass.name) + attrs += @@editable_attributes[klass.name] + klass = klass.superclass + end + attrs + end + def inspect_validations(for_json = false) validators.inject({}) do |memo, validator| if validator.respond_to? :attributes diff --git a/spec/schema_generator_spec.rb b/spec/schema_generator_spec.rb index 7ed39e0..9fa2b70 100644 --- a/spec/schema_generator_spec.rb +++ b/spec/schema_generator_spec.rb @@ -30,11 +30,21 @@ Example.schema[:validations][:code].should have_key :presence Example.schema[:validations][:code].should have_key :numericality end + + it 'includes editable attributes' do + Example.schema[:editable_attributes].size.should == 2 + Example.schema[:editable_attributes].should include(:name, :code) + end + + it 'excludes non-editable attributes' do + Example.schema[:editable_attributes].should_not include(:created_at) + end end class Example < Deja::Node attribute :name, String attribute :code, String + attribute :created_at, Time, :editable => false validates :name, :presence => true validates :code, :presence => true