From f5c052e50999c99b32fe6b9594543f14e587d8a5 Mon Sep 17 00:00:00 2001 From: Todd Eichel Date: Thu, 29 Jan 2015 17:47:21 -0800 Subject: [PATCH 1/2] Add the ability to define uneditable attributes in an Upmin::Model. --- lib/upmin/active_record/model.rb | 4 ++++ lib/upmin/attribute.rb | 12 +----------- lib/upmin/model.rb | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/upmin/active_record/model.rb b/lib/upmin/active_record/model.rb index e3e9688..83d2e86 100644 --- a/lib/upmin/active_record/model.rb +++ b/lib/upmin/active_record/model.rb @@ -25,6 +25,10 @@ def default_attributes return model_class.attribute_names.map(&:to_sym) end + def default_uneditable_attributes + return [:id, :created_at, :created_on, :updated_at, :updated_on] + end + def attribute_type(attribute) adapter = model_class.columns_hash[attribute.to_s] if adapter diff --git a/lib/upmin/attribute.rb b/lib/upmin/attribute.rb index 5d68001..0123a6d 100644 --- a/lib/upmin/attribute.rb +++ b/lib/upmin/attribute.rb @@ -29,19 +29,9 @@ def type end def editable? - case name.to_sym - when :id - return false - when :created_at - return false - when :created_on - return false - when :updated_at - return false - when :updated_on + if model.uneditable_attributes.include? name return false else - # TODO(jon): Add a way to declare which attributes are editable and which are not later. return model.respond_to?("#{name}=") end end diff --git a/lib/upmin/model.rb b/lib/upmin/model.rb index d38ba33..99baf7a 100644 --- a/lib/upmin/model.rb +++ b/lib/upmin/model.rb @@ -50,6 +50,12 @@ def attributes return @attributes end + def uneditable_attributes + return @uneditable_attributes if defined?(@uneditable_attributes) + @uneditable_attributes = self.class.uneditable_attributes + return @uneditable_attributes + end + def associations return @associations if defined?(@associations) @associations = [] @@ -258,6 +264,18 @@ def Model.attributes(*attributes) return (@attributes + @extra_attrs).uniq end + # Sets the uneditable attributes to the provided attributes merged with the + # default uneditable attributes. + def Model.uneditable_attributes(*uneditable_attributes) + @uneditable_attributes = [] unless defined?(@uneditable_attributes) + + if uneditable_attributes.any? + @uneditable_attributes = uneditable_attributes.map{|a| a.to_sym} + end + + return (default_uneditable_attributes + @uneditable_attributes).uniq + end + # Add a single action to upmin actions. If this is called # before upmin_actions the actions will not include any defaults # actions. @@ -304,6 +322,11 @@ def Model.default_attributes return default_attributes end + def Model.default_uneditable_attributes + new + return default_uneditable_attributes + end + def Model.attribute_type(attribute) new return attribute_type(attribute) From 7aea17540f0684ea099780de1579efe7e8b865ed Mon Sep 17 00:00:00 2001 From: Todd Eichel Date: Wed, 11 Feb 2015 14:43:56 -0800 Subject: [PATCH 2/2] Move `default_uneditable_attributes` out of ORM adapters so declaring uneditable attributes in an admin class definition does not result in an instance of the model class being instantiated. --- lib/upmin/active_record/model.rb | 4 ---- lib/upmin/model.rb | 9 ++++----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/upmin/active_record/model.rb b/lib/upmin/active_record/model.rb index 83d2e86..e3e9688 100644 --- a/lib/upmin/active_record/model.rb +++ b/lib/upmin/active_record/model.rb @@ -25,10 +25,6 @@ def default_attributes return model_class.attribute_names.map(&:to_sym) end - def default_uneditable_attributes - return [:id, :created_at, :created_on, :updated_at, :updated_on] - end - def attribute_type(attribute) adapter = model_class.columns_hash[attribute.to_s] if adapter diff --git a/lib/upmin/model.rb b/lib/upmin/model.rb index 99baf7a..9900359 100644 --- a/lib/upmin/model.rb +++ b/lib/upmin/model.rb @@ -195,6 +195,10 @@ def Model.search_path return Upmin::Engine.routes.url_helpers.upmin_search_path(klass: model_class_name) end + def Model.default_uneditable_attributes + return [:id, :created_at, :created_on, :updated_at, :updated_on] + end + def Model.color return @color if defined?(@color) @color = Model.next_color @@ -322,11 +326,6 @@ def Model.default_attributes return default_attributes end - def Model.default_uneditable_attributes - new - return default_uneditable_attributes - end - def Model.attribute_type(attribute) new return attribute_type(attribute)