Permalink
Browse files

Re-factor duplicated code.

  • Loading branch information...
1 parent 45e3414 commit 2333c8f33fbcfffeeb5b140d2da99cef7423244d @markstory markstory committed Apr 11, 2012
Showing with 21 additions and 22 deletions.
  1. +21 −22 lib/Cake/Model/Behavior/TranslateBehavior.php
@@ -471,17 +471,7 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
);
}
- if (array_key_exists($field, $this->settings[$model->alias])) {
- unset($this->settings[$model->alias][$field]);
- } elseif (in_array($field, $this->settings[$model->alias])) {
- $this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
- }
-
- if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
- unset($this->runtime[$model->alias]['fields'][$field]);
- } elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
- $this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
- }
+ $this->_updateSettings($model, $field);
if (is_null($association)) {
if ($reset) {
@@ -518,6 +508,25 @@ public function bindTranslation(Model $model, $fields, $reset = true) {
return true;
}
+/**
+ * Update runtime setting for a given field.
+ *
+ * @param string $field The field to update.
+ */
+ protected function _updateSettings(Model $model, $field) {
+ if (array_key_exists($field, $this->settings[$model->alias])) {
+ unset($this->settings[$model->alias][$field]);
+ } elseif (in_array($field, $this->settings[$model->alias])) {
+ $this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
+ }
+
+ if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
+ unset($this->runtime[$model->alias]['fields'][$field]);
+ } elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
+ $this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
+ }
+ }
+
/**
* Unbind translation for fields, optionally unbinds hasMany association for
* fake field
@@ -550,17 +559,7 @@ public function unbindTranslation(Model $model, $fields = null) {
$association = $value;
}
- if (array_key_exists($field, $this->settings[$model->alias])) {
- unset($this->settings[$model->alias][$field]);
- } elseif (in_array($field, $this->settings[$model->alias])) {
- $this->settings[$model->alias] = array_merge(array_diff_assoc($this->settings[$model->alias], array($field)));
- }
-
- if (array_key_exists($field, $this->runtime[$model->alias]['fields'])) {
- unset($this->runtime[$model->alias]['fields'][$field]);
- } elseif (in_array($field, $this->runtime[$model->alias]['fields'])) {
- $this->runtime[$model->alias]['fields'] = array_merge(array_diff_assoc($this->runtime[$model->alias]['fields'], array($field)));
- }
+ $this->_updateSettings($model, $field);
if (!is_null($association) && (isset($model->hasMany[$association]) || isset($model->__backAssociation['hasMany'][$association]))) {
$associations[] = $association;

0 comments on commit 2333c8f

Please sign in to comment.