nighty commit?

This commit is contained in:
Parziphal 2013-10-09 00:30:48 -05:00
parent cb2a2f9c1d
commit 144c021616
8 changed files with 92 additions and 66 deletions

View File

@ -214,7 +214,7 @@ abstract class Base extends ActionController
public function action_name()
{
return Rails::services()->get('inflector')->camelize(Rails::application()->dispatcher()->router()->route()->action, true);
return Rails::services()->get('inflector')->camelize(Rails::application()->dispatcher()->router()->route()->action, false);
}
public function run_request_action()

View File

@ -40,6 +40,7 @@ class ViewHelpers
return $helper;
}
}
return false;
}

View File

@ -11,7 +11,7 @@ use Rails\ActiveModel;
abstract class Base
{
use Methods\CounterMethods, Methods\RelationMethods, Methods\ScopingMethods,
Methods\AttributeMethods, Methods\ModelSchemaMethods;
Methods\AttributeMethods, Methods\ModelSchemaMethods, Methods\AssociationMethods;
/**
* ActiveRecord_Registry instance.
@ -40,12 +40,6 @@ abstract class Base
*/
private $isNewRecord = true;
/**
* An array where loaded associations will
* be stored.
*/
private $loadedAssociations = [];
static public function __callStatic($method, $params)
{
if ($rel = static::scope($method, $params))
@ -136,6 +130,11 @@ abstract class Base
return self::connection()->selectValue('SELECT MAX(' . $attr . ') FROM ' . static::tableName());
}
static public function count()
{
return self::connection()->selectValue('SELECT COUNT(*) FROM `' . self::tableName() . '`');
}
static public function I18n()
{
return Rails::application()->I18n();
@ -523,23 +522,13 @@ abstract class Base
$builder->build($attrs, $params);
}
}
public function getAssociation($name)
{
if (isset($this->loadedAssociations[$name])) {
return $this->loadedAssociations[$name];
} elseif ($assoc = $this->get_association_data($name)) {
$model = $this->_load_association($name, $assoc[0], $assoc[1]);
$this->loadedAssociations[$name] = $model;
return $this->loadedAssociations[$name];
}
}
/**
* ***************************
* Default protected methods {
* ***************************
* attrAccessible and attrProtected can be found in Base\Methods\AttributeMethods.
* associations can be found in Base\Methods\AssociationMethods
*/
/**
@ -550,11 +539,6 @@ abstract class Base
{
}
protected function associations()
{
return [];
}
/**
* Example:
*
@ -648,14 +632,6 @@ abstract class Base
return true;
}
/**
* @param array|Closure $params - Additional parameters to customize the query for the association
*/
private function _load_association($prop, $type, $params)
{
return $this->{'_find_' . $type}($prop, $params);
}
private function _get_parents_callbacks($callback_name)
{
$all_callbacks = array();
@ -673,17 +649,6 @@ abstract class Base
return $all_callbacks;
}
# Returns association property names.
private function _associations_names()
{
$associations = array();
foreach ($this->associations() as $assocs) {
foreach ($assocs as $k => $v)
$associations[] = is_int($k) ? $v : $k;
}
return $associations;
}
/**
* @return bool
* @see validations()
@ -937,25 +902,6 @@ abstract class Base
return true;
}
private function get_association_data($prop)
{
if ($assocs = $this->associations()) {
foreach ($assocs as $type => $assoc) {
foreach ($assoc as $name => $params) {
if (is_int($name)) {
$name = $params;
$params = array();
}
if ($name == $prop) {
return array($type, $params);
}
}
}
}
return false;
}
private function _register()
{
self::_registry()->register($this);

View File

@ -1 +1,77 @@
<?php
namespace Rails\ActiveRecord\Base\Methods;
use Rails\ActiveRecord\Exception;
trait AssociationMethods
{
/**
* An array where loaded associations will
* be stored.
*/
private $loadedAssociations = [];
protected function associations()
{
return [];
}
public function getAssociation($name)
{
if (isset($this->loadedAssociations[$name])) {
return $this->loadedAssociations[$name];
} elseif ($assoc = $this->get_association_data($name)) {
$model = $this->_load_association($name, $assoc[0], $assoc[1]);
$this->loadedAssociations[$name] = $model;
return $this->loadedAssociations[$name];
}
}
protected function setAssociation($name, $object)
{
if (!in_array($name, $this->_associations_names())) {
throw new Exception\RuntimeException(
sprintf("Tried to set unknown association: %s", $name)
);
}
$this->loadedAssociations[$name] = $object;
}
# Returns association property names.
private function _associations_names()
{
$associations = array();
foreach ($this->associations() as $assocs) {
foreach ($assocs as $k => $v)
$associations[] = is_int($k) ? $v : $k;
}
return $associations;
}
/**
* @param array|Closure $params - Additional parameters to customize the query for the association
*/
private function _load_association($prop, $type, $params)
{
return $this->{'_find_' . $type}($prop, $params);
}
private function get_association_data($prop)
{
if ($assocs = $this->associations()) {
foreach ($assocs as $type => $assoc) {
foreach ($assoc as $name => $params) {
if (is_int($name)) {
$name = $params;
$params = array();
}
if ($name == $prop) {
return array($type, $params);
}
}
}
}
return false;
}
}

View File

@ -61,7 +61,7 @@ trait AttributeMethods
// if (!Rails::config()->ar2) {
// return static::table()->columnExists(static::properAttrName($name));
// } else {
return static::table()->columnExists($name);
return static::table()->columnExists($name);
// }
}

View File

@ -170,7 +170,9 @@ trait RelationMethods
*/
private function _find_has_many($prop, $params)
{
empty($params['class_name']) && $params['class_name'] = rtrim(ucfirst($prop), 's');
$inflector = \Rails::services()->get('inflector');
empty($params['class_name']) && $params['class_name'] = $inflector->camelize($inflector->singularize($prop));
$builder = new Association($params, $this);
$builder->build_query();

View File

@ -27,8 +27,9 @@ class Association extends AbstractRelation
$params = $this->params;
if (empty($params['foreign_key'])) {
$inflector = \Rails::services()->get('inflector');
$cn = get_class($this->parent_model);
$params['foreign_key'] = substr($cn::tableName(), 0, -1).'_id';
$params['foreign_key'] = $inflector->singularize($cn::tableName()).'_id';
}
$query = new Relation($params['class_name'], $params['class_name']::tableName());

View File

@ -147,7 +147,7 @@ class Inflector
public function classify($tableName)
{
return $this->camelize($this->singuralize(preg_replace('/.*\./', '', $tableName)));
return $this->camelize($this->singularize(preg_replace('/.*\./', '', $tableName)));
}
public function ordinal($number)