Skip to content

Commit

Permalink
Merge pull request maxdemarzi#105 from vsamaco/strong-params
Browse files Browse the repository at this point in the history
Added editable options for strong params
  • Loading branch information
anwnguyen committed Feb 6, 2014
2 parents 998a419 + b20093a commit 5ffd26d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/deja/schema_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand All @@ -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)
Expand All @@ -36,6 +40,10 @@ def attr_accessorize(name, opts)
end
end

def editable_attributes
@@editable_attributes
end

def indexed_attributes
@@indexed_attributes
end
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/schema_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ffd26d

Please sign in to comment.