active_fedora-registered_attributes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8dac8cbef5f9c4b50b43a392200ac5ad508c946a
4
- data.tar.gz: 24c9889b73ad5b4cd584323c7a654d72877ab4ca
3
+ metadata.gz: 1f3ebbd73ebefa0ab6879e1c931ba1e910d57cc3
4
+ data.tar.gz: 9f3e8bffeb4f9a856436ab3eeb14fc845481613e
5
5
  SHA512:
6
- metadata.gz: 5c056a733cd092784c5b730fe233c1988c3cb65e2cc116a7ab36e1cfff5c595fd2337492e78cf6b07c5eeba5ecb064fc6a1d602508283abbb9f75ae4857b18fd
7
- data.tar.gz: 2bf802fc4d7178567339b552a84a51e12f8befe12cfdff0163371b5012d55c729ee19360f700fb3e0daa2184af66f0b278185e5698f92d5254da56617709c805
6
+ metadata.gz: 9f3dbba63bb918a6ba656116f3d04336ba738b1f7af4928bff84eb538c665d3d09fcefc4760e15b4331ac9404827197e4c4f87c3a5005b23cb18591071c6c37f
7
+ data.tar.gz: be2a203b103986dc7ed68f03c6f2de2770be7719fc932c74bbfaa88de0c79033e7ca0b1cc9dff8c5020e77c68e9b6f7f75c69b0d20d29d65b6b5f1fbf67ea26f
data/README.md CHANGED
@@ -4,6 +4,13 @@
4
4
 
5
5
  An ActiveFedora extension for consolidating the attribute definitions for an object.
6
6
 
7
+ By registering an attribute, we can introspect on the model for:
8
+
9
+ * generating forms
10
+ * generating default views
11
+ * generating reasonably helpful API documentation/explanation
12
+ * helping developers readily see what the data attributes are
13
+
7
14
  ## Installation
8
15
 
9
16
  Add this line to your application's Gemfile:
@@ -39,6 +46,8 @@ Or install it yourself as:
39
46
  validates: { presence: { message: "You must have an author."} }
40
47
  end
41
48
 
49
+ See [ActiveFedora::RegisteredAttributes::Attribute](lib/active_fedora/registered_attributes/attribute.rb)
50
+
42
51
  ## Internationalization
43
52
 
44
53
  If you utilize internationalization, such as below, then regardless of what
@@ -5,10 +5,41 @@ module ActiveFedora
5
5
  attr_reader :context_class, :name, :datastream
6
6
  private :context_class
7
7
 
8
+ # == Parameters:
9
+ # @param context_class [ActiveFedora::Base, #human_attribute_name]
10
+ # A descendant of ActiveFedora::Base.
11
+ # Though generally speaking it may work with other ActiveModel descendants
12
+ #
13
+ # @param name [String, Symbol]
14
+ # The name of the attribute (i.e. "title", "subject", "description").
15
+ # @param [Hash] options Configuration options
16
+ # @option options [Symbol, #call] :default
17
+ # @option options [Boolean] :displayable (true)
18
+ # @option options [Boolean] :editable (true)
19
+ # By marking this attribute :editable
20
+ # @option options [Hash] :form
21
+ # Additional options for a form builder (i.e. class, id, data-attribute)
22
+ # @option options [Symbol, String, Nil, Hash] :datastream
23
+ # Where will the attribute persist; This can be nil. If nil, this would
24
+ # be a virtual attribute (i.e. attr_accessor name). If it is not nil,
25
+ # then see {#options_for_delegation}
26
+ # @option options [Hash] :validates
27
+ # A hash that can be used as the args for ActiveModel::Validations::ClassMethods.validates
28
+ # @option options [Boolean] :multiple (false)
29
+ # Can there be multiple values for this attribute?
30
+ # Used to derive an option for ActiveFedora::Base.delegate
31
+ # @option options [Symbol, #call] :writer
32
+ # Before we persist the attribute, pass the value through the :writer
33
+ # @option options [Symbol, #call] :reader
34
+ # After we retrieve the value from its persistence, transform the value via the :reader
35
+ # @option options [#to_s] :label (internationalization)
36
+ # If we were to build a form from this, what would we use as the label
37
+ # @option options [#to_s] :hint
38
+ # A supplement to the Attribute's :label
8
39
  def initialize(context_class, name, options = {})
9
40
  @context_class = context_class
10
41
  @options = options.symbolize_keys
11
- @options.assert_valid_keys(:default, :displayable, :editable, :form, :datastream, :validates, :at, :as, :multiple, :writer, :reader, :label, :hint)
42
+ @options.assert_valid_keys(:default, :displayable, :editable, :form, :datastream, :validates, :multiple, :writer, :reader, :label, :hint)
12
43
  @datastream = @options.fetch(:datastream, false)
13
44
  @name = name
14
45
  @options[:multiple] = false unless @options.key?(:multiple)
@@ -44,7 +75,6 @@ module ActiveFedora
44
75
  options[:form].tap {|hash|
45
76
  hash[:hint] ||= options[:hint] if options[:hint]
46
77
  hash[:label] ||= options[:label] if options[:label]
47
- hash[:as] ||= options[:as] if options[:as]
48
78
  if options[:multiple]
49
79
  hash[:as] = 'multi_value'
50
80
  hash[:input_html] ||= {}
@@ -87,12 +117,14 @@ module ActiveFedora
87
117
  private
88
118
 
89
119
  def options_for_delegation
90
- {
91
- to: datastream,
92
- unique: !options[:multiple]
93
- }.tap {|hash|
94
- hash[:at] = options[:at] if options.key?(:at)
95
- }
120
+ if datastream.is_a?(Hash)
121
+ datastream.merge(unique: !options[:multiple])
122
+ else
123
+ {
124
+ to: datastream,
125
+ unique: !options[:multiple]
126
+ }
127
+ end
96
128
  end
97
129
 
98
130
  def with_writer_method_wrap
@@ -1,5 +1,5 @@
1
1
  module ActiveFedora
2
2
  module RegisteredAttributes
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -47,14 +47,40 @@ describe ActiveFedora::RegisteredAttributes::Attribute do
47
47
 
48
48
  describe '#with_delegation_options' do
49
49
  describe 'with a datastream' do
50
- it 'yields name and options' do
51
- @yielded = false
52
- subject.with_delegation_options {|name,opts|
53
- @yielded = true
54
- expect(name).to eq(field_name)
55
- expect(opts).to eq(subject.send(:options_for_delegation))
50
+ let(:options) {
51
+ {
52
+ datastream: datastream,
53
+ multiple: true,
56
54
  }
57
- expect(@yielded).to eq(true)
55
+ }
56
+ describe 'and minimal options' do
57
+ it 'yields name and options' do
58
+ @yielded = false
59
+ subject.with_delegation_options {|name,opts|
60
+ @yielded = true
61
+ expect(name).to eq(field_name)
62
+ expect(opts).to eq(to: datastream, unique: false)
63
+ }
64
+ expect(@yielded).to eq(true)
65
+ end
66
+ end
67
+ describe 'with :at options' do
68
+ let(:at_value) {[:ab]}
69
+ let(:options) {
70
+ {
71
+ multiple: true,
72
+ datastream: {to: datastream, at: at_value, unique: true }
73
+ }
74
+ }
75
+ it 'yields name and options' do
76
+ @yielded = false
77
+ subject.with_delegation_options {|name,opts|
78
+ @yielded = true
79
+ expect(name).to eq(field_name)
80
+ expect(opts).to eq(to: datastream, unique: !options.fetch(:multiple), at: at_value)
81
+ }
82
+ expect(@yielded).to eq(true)
83
+ end
58
84
  end
59
85
  end
60
86
  describe 'without datastream' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_fedora-registered_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Friesen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-16 00:00:00.000000000 Z
11
+ date: 2013-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active-fedora