diff --git a/lib/Rails/ActionController/Response/Partial.php b/lib/Rails/ActionController/Response/Partial.php index 0d7d2d3..dbf6491 100755 --- a/lib/Rails/ActionController/Response/Partial.php +++ b/lib/Rails/ActionController/Response/Partial.php @@ -4,26 +4,40 @@ namespace Rails\ActionController\Response; use Rails\ActionView; # TODO -class ActionController_Response_Partial extends Base +class Partial extends Base { + private $_template; + public function _render_view() { - $params = [$this->_params['partial']]; - if (isset($this->_params['locals'])) - $params = array_merge($params, [$this->_params['locals']]); - # Include helpers. ActionView\ViewHelpers::load(); + $layout = !empty($this->_params['layout']) ? $this->_params['layout'] : false; + $partial = $this->_params['partial']; + $locals = (array)\Rails::application()->controller()->locals(); + $this->_template = new ActionView\Template(['lambda' => function() use ($partial, $locals) { + echo $this->partial($partial, $locals); + }], ['layout' => $layout]); + + // $this->_template->setLocals(); + + $this->_template->renderContent(); + // $params = [$this->_params['partial']]; + // if (isset($this->_params['locals'])) + // $params = array_merge($params, [$this->_params['locals']]); + + # Include helpers. + // ActionView\ViewHelpers::load(); # Create a template so we can call render_partial. # This shouldn't be done this way. - $template = new ActionView\Template([]); - - $this->_body = call_user_func_array([$template, 'render_partial'], $params); + // $template = new ActionView\Template([]); + // vpe($this); + // $this->_body = call_user_func_array([$template, 'renderContent'], $params); } public function _print_view() { - return $this->_body; + return $this->_template->content(); } -} \ No newline at end of file +} diff --git a/lib/Rails/ActionDispatch/Http/Exception/ExceptionInterface.php b/lib/Rails/ActionDispatch/Http/Exception/ExceptionInterface.php deleted file mode 100755 index c946589..0000000 --- a/lib/Rails/ActionDispatch/Http/Exception/ExceptionInterface.php +++ /dev/null @@ -1,6 +0,0 @@ - $row['Type']]; + $data = [ + 'type' => $row['Type'], + 'default' => $row['Default'] + ]; if (strpos($row['Type'], 'enum') === 0) { $enum_values = []; @@ -52,4 +55,4 @@ class Table/* extends AbstractTable*/ return [$table_data, $table_indexes]; } -} \ No newline at end of file +} diff --git a/lib/Rails/ActiveRecord/Adapter/Sqlite/Table.php b/lib/Rails/ActiveRecord/Adapter/Sqlite/Table.php index a151d53..1c17ded 100755 --- a/lib/Rails/ActiveRecord/Adapter/Sqlite/Table.php +++ b/lib/Rails/ActiveRecord/Adapter/Sqlite/Table.php @@ -24,7 +24,10 @@ class Table/* extends AbstractTable*/ ]; foreach ($rows as $row) { - $data = ['type' => $row['type']]; + $data = [ + 'type' => $row['type'], + 'default' => $row['default'] + ]; $table_data[$row['name']] = $data; if ($row['pk']) @@ -33,4 +36,4 @@ class Table/* extends AbstractTable*/ return [$table_data, $table_indexes]; } -} \ No newline at end of file +} diff --git a/lib/Rails/ActiveRecord/Base.php b/lib/Rails/ActiveRecord/Base.php index ccb5727..c5852ed 100755 --- a/lib/Rails/ActiveRecord/Base.php +++ b/lib/Rails/ActiveRecord/Base.php @@ -30,6 +30,11 @@ abstract class Base */ static private $preventInit = false; + /** + * Flag to prevent setting default attributes. + */ + static private $skipDefaultAttributes = false; + /** * ActiveModel\Errors instance. */ @@ -205,6 +210,7 @@ abstract class Base static private function _create_model(array $data) { self::$preventInit = true; + self::$skipDefaultAttributes = true; $model = new static(); $model->attributes = $data; @@ -243,6 +249,12 @@ abstract class Base public function __construct(array $attrs = []) { + if (!self::$skipDefaultAttributes) { + $this->setDefaultAttributes(); + } else { + self::$skipDefaultAttributes = true; + } + $this->assignAttributes($attrs); if (!self::$preventInit) { $this->init(); diff --git a/lib/Rails/ActiveRecord/Base/Methods/AttributeMethods.php b/lib/Rails/ActiveRecord/Base/Methods/AttributeMethods.php index 68bddba..cd9f329 100755 --- a/lib/Rails/ActiveRecord/Base/Methods/AttributeMethods.php +++ b/lib/Rails/ActiveRecord/Base/Methods/AttributeMethods.php @@ -282,4 +282,9 @@ trait AttributeMethods $attributes = array_diff_key($attributes, array_fill_keys($attrs, true)); } } -} \ No newline at end of file + + private function setDefaultAttributes() + { + $this->attributes = self::table()->columnDefaults(); + } +} diff --git a/lib/Rails/ActiveRecord/ModelSchema.php b/lib/Rails/ActiveRecord/ModelSchema.php index 94b1327..7d322e9 100755 --- a/lib/Rails/ActiveRecord/ModelSchema.php +++ b/lib/Rails/ActiveRecord/ModelSchema.php @@ -17,6 +17,8 @@ class ModelSchema protected $primaryKey; + protected $columnDefaults; + public function __construct($name, Connection $connection) { $this->name = $name; @@ -104,6 +106,19 @@ class ModelSchema return !empty($this->columns[$column_name]); } + public function columnDefaults() + { + if ($this->columnDefaults === null) { + $this->columnDefaults = []; + foreach ($this->columns as $name => $data) { + if ($data['default'] !== null) { + $this->columnDefaults[$name] = $data['default']; + } + } + } + return $this->columnDefaults; + } + public function enumValues($column_name) { if (!isset($this->columns[$column_name])) { diff --git a/lib/Rails/Exception/LogicException.php b/lib/Rails/Exception/LogicException.php index 15cb10f..6cbeb7c 100755 --- a/lib/Rails/Exception/LogicException.php +++ b/lib/Rails/Exception/LogicException.php @@ -4,4 +4,4 @@ namespace Rails\Exception; class LogicException extends \LogicException implements ExceptionInterface { use ExceptionTrait; -} \ No newline at end of file +} diff --git a/lib/Rails/I18n/I18n.php b/lib/Rails/I18n/I18n.php index d8395e4..5c35396 100755 --- a/lib/Rails/I18n/I18n.php +++ b/lib/Rails/I18n/I18n.php @@ -74,6 +74,14 @@ class I18n return false; } + /** + * When adding new translations with the same name, since the arrays are + * recursively merged, the result will not be a string, but an array. + * If this is the case, the latter value will be used. + */ + if (is_array($tr)) + $tr = array_pop($tr); + if (is_int(strpos($tr, '%{'))) { foreach ($params as $k => $param) { $tr = str_replace('%{'.$k.'}', $param, $tr); diff --git a/lib/Rails/Toolbox/DbTools.php b/lib/Rails/Toolbox/DbTools.php index 786dff0..1d5fd75 100755 --- a/lib/Rails/Toolbox/DbTools.php +++ b/lib/Rails/Toolbox/DbTools.php @@ -50,4 +50,4 @@ class DbTools } } } -} \ No newline at end of file +}