Skip to content

Commit

Permalink
Move field preprocessor documentation into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Jul 25, 2021
1 parent 1b73df1 commit ba6be0d
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 188 deletions.
189 changes: 1 addition & 188 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,194 +307,7 @@ This example will copy the post's configuration data and keep tags associated wi

### Field Preprocessors

#### Nullify

If you wish to prevent a regular (non `has_*` association based) field from retaining it's value when copied, you may "zero out" or "nullify" the field, like this:

```ruby
class Topic < ActiveRecord::Base
has_many :posts
end

class Post < ActiveRecord::Base
belongs_to :topic
has_many :comments

amoeba do
enable
nullify :date_published
nullify :topic_id
end
end

class Comment < ActiveRecord::Base
belongs_to :post
end
```

This example will copy all of a post's comments. It will also nullify the publishing date and dissociate the post from its original topic.

Unlike inclusive and exclusive styles, specifying null fields will not automatically enable amoeba to copy all child records. As with any active record object, the default field value will be used instead of `nil` if a default value exists on the migration.

#### Set

If you wish to just set a field to an arbitrary value on all duplicated objects you may use the `set` directive. For example, if you wanted to copy an object that has some kind of approval process associated with it, you likely may wish to set the new object's state to be open or "in progress" again.

```ruby
class Post < ActiveRecord::Base
amoeba do
set :state_tracker => "open_for_editing"
end
end
```

In this example, when a post is duplicated, it's `state_tracker` field will always be given a value of `open_for_editing` to start.

#### Prepend

You may add a string to the beginning of a copied object's field during the copy phase:

```ruby
class Post < ActiveRecord::Base
amoeba do
enable
prepend :title => "Copy of "
end
end
```

#### Append

You may add a string to the end of a copied object's field during the copy phase:

```ruby
class Post < ActiveRecord::Base
amoeba do
enable
append :title => "Copy of "
end
end
```

#### Regex

You may run a search and replace query on a copied object's field during the copy phase:

```ruby
class Post < ActiveRecord::Base
amoeba do
enable
regex :contents => {:replace => /dog/, :with => 'cat'}
end
end
```

#### Custom Methods

##### Customize

You may run a custom method or methods to do basically anything you like, simply pass a lambda block, or an array of lambda blocks to the `customize` directive. Each block must have the same form, meaning that each block must accept two parameters, the original object and the newly copied object. You may then do whatever you wish, like this:

```ruby
class Post < ActiveRecord::Base
amoeba do
prepend :title => "Hello world! "

customize(lambda { |original_post,new_post|
if original_post.foo == "bar"
new_post.baz = "qux"
end
})

append :comments => "... know what I'm sayin?"
end
end
```

or this, using an array:

```ruby
class Post < ActiveRecord::Base
has_and_belongs_to_many :tags

amoeba do
include_association :tags

customize([
lambda do |orig_obj,copy_of_obj|
# good stuff goes here
end,

lambda do |orig_obj,copy_of_obj|
# more good stuff goes here
end
])
end
end
```

##### Override

Lambda blocks passed to customize run, by default, after all copying and field pre-processing. If you wish to run a method before any customization or field pre-processing, you may use `override` the cousin of `customize`. Usage is the same as above.

```ruby
class Post < ActiveRecord::Base
amoeba do
prepend :title => "Hello world! "

override(lambda { |original_post,new_post|
if original_post.foo == "bar"
new_post.baz = "qux"
end
})

append :comments => "... know what I'm sayin?"
end
end
```

#### Chaining

You may apply a single preprocessor to multiple fields at once.

```ruby
class Post < ActiveRecord::Base
amoeba do
enable
prepend :title => "Copy of ", :contents => "Copied contents: "
end
end
```

#### Stacking

You may apply multiple preprocessing directives to a single model at once.

```ruby
class Post < ActiveRecord::Base
amoeba do
prepend :title => "Copy of ", :contents => "Original contents: "
append :contents => " (copied version)"
regex :contents => {:replace => /dog/, :with => 'cat'}
end
end
```

This example should result in something like this:

```ruby
post = Post.create(
:title => "Hello world",
:contents => "I like dogs, dogs are awesome."
)

new_post = post.amoeba_dup

new_post.title # "Copy of Hello world"
new_post.contents # "Original contents: I like cats, cats are awesome. (copied version)"
```

Like `nullify`, the preprocessing directives do not automatically enable the copying of associated child records. If only preprocessing directives are used and you do want to copy child records and no `include_association` or `exclude_association` list is provided, you must still explicitly enable the copying of child records by calling the enable method from within the amoeba block on your model.
See [Field Preprocessors](docs/field_preprocessors.md)

### Precedence

Expand Down
Loading

0 comments on commit ba6be0d

Please sign in to comment.