lib/alchemy/resource.rb in alchemy_cms-3.1.0.beta5 vs lib/alchemy/resource.rb in alchemy_cms-3.1.0.beta6

- old
+ new

@@ -18,11 +18,11 @@ # class is named differently (or sits in another namespace) model and controller are handled separatly here. # Therefore "resource" always refers to the controller_path whereas "model" refers to the model class. # # == Skip attributes # - # Usually you don't want your users to edit all attributes provided by a model. Hence some default attributes, + # Usually you don't want your users to see and edit all attributes provided by a model. Hence some default attributes, # namely id, updated_at, created_at, creator_id and updater_id are not returned by Resource#attributes. # # If you want to skip a different set of attributes just define a +skipped_alchemy_resource_attributes+ class method in your model class # that returns an array of strings. # @@ -30,10 +30,21 @@ # # def self.skipped_alchemy_resource_attributes # %w(id updated_at secret_token remote_ip) # end # + # == Restrict attributes + # + # Beside skipping certain attributes you can also restrict them. Restricted attributes can not be edited by the user but still be seen in the index view. + # No attributes are restricted by default. + # + # === Example + # + # def self.restricted_alchemy_resource_attributes + # %w(synced_at remote_record_id) + # end + # # == Resource relations # # Alchemy::Resource can take care of ActiveRecord relations. # # === BelongsTo Relations @@ -71,21 +82,22 @@ # a model class (for example used by Alchemy's Tags admin interface): # # resource = Resource.new('/admin/tags', {"engine_name"=>"alchemy"}, ActsAsTaggableOn::Tag) # class Resource - attr_accessor :skipped_attributes, :resource_relations, :model_associations + attr_accessor :skipped_attributes, :restricted_attributes, :resource_relations, :model_associations attr_reader :model DEFAULT_SKIPPED_ATTRIBUTES = %w(id updated_at created_at creator_id updater_id) DEFAULT_SKIPPED_ASSOCIATIONS = %w(creator updater) def initialize(controller_path, module_definition=nil, custom_model=nil) @controller_path = controller_path @module_definition = module_definition @model = (custom_model or guess_model_from_controller_path) self.skipped_attributes = model.respond_to?(:skipped_alchemy_resource_attributes) ? model.skipped_alchemy_resource_attributes : DEFAULT_SKIPPED_ATTRIBUTES + self.restricted_attributes = model.respond_to?(:restricted_alchemy_resource_attributes) ? model.restricted_alchemy_resource_attributes : [] if model.respond_to?(:alchemy_resource_relations) if not model.respond_to?(:reflect_on_all_associations) raise MissingActiveRecordAssociation end store_model_associations @@ -135,9 +147,13 @@ type: resource_column_type(col), relation: resource_relation(col.name) }.delete_if { |k, v| v.nil? } end end.compact + end + + def editable_attributes + attributes.reject { |h| self.restricted_attributes.map(&:to_s).include?(h[:name].to_s) } end # Returns all columns that are searchable # # For now it only uses string type columns