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..9900359 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 = [] @@ -189,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 @@ -258,6 +268,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.